<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Margaret Teague</title>
    <description>The latest articles on DEV Community by Margaret Teague (@margaretincali).</description>
    <link>https://dev.to/margaretincali</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1169145%2F648501fd-67b9-4777-8f32-b59b17f25a79.jpeg</url>
      <title>DEV Community: Margaret Teague</title>
      <link>https://dev.to/margaretincali</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/margaretincali"/>
    <language>en</language>
    <item>
      <title>How to Delete Files in Python</title>
      <dc:creator>Margaret Teague</dc:creator>
      <pubDate>Wed, 17 Jan 2024 20:00:00 +0000</pubDate>
      <link>https://dev.to/margaretincali/how-to-delete-files-in-python-1g7b</link>
      <guid>https://dev.to/margaretincali/how-to-delete-files-in-python-1g7b</guid>
      <description>Photo by &lt;a href="https://unsplash.com/@rwlinder?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash"&gt;Robert Linder&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/a-row-of-trash-cans-sitting-next-to-a-brick-wall-JLoE-DntHtY?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash"&gt;Unsplash&lt;/a&gt;
  

&lt;p&gt;Like, &lt;strong&gt;&lt;em&gt;lots&lt;/em&gt;&lt;/strong&gt; and lots of files...&lt;/p&gt;




&lt;p&gt;I work as a web developer for a CRM that caters primarily to automotive dealerships. We receive thousands of customer leads each day from dozens of advertising sources.&lt;/p&gt;

&lt;p&gt;We've recently realized that we have grown to such a size, that having thousands and thousands of email copies hanging out on our servers was starting to drag down on some performance times. So we decided to delete all emailed leads older than two weeks. I volunteered to complete this task twice monthly.&lt;/p&gt;

&lt;p&gt;Friends, I was doing this manually. In other words, I would highlight 5,000 or so emails in the file window at a time and hit &lt;code&gt;DELETE&lt;/code&gt;. And then I would have to delete them again by emptying the recycle bin.&lt;/p&gt;

&lt;p&gt;This task would often take me an hour to complete.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExd2pmaGM1aGt0Y25kbWlscGZmajNmMnlvZ3czYW91NWFxd3YybmoxMiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/whQCarjn5Jv1Ktq2HH/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExd2pmaGM1aGt0Y25kbWlscGZmajNmMnlvZ3czYW91NWFxd3YybmoxMiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/whQCarjn5Jv1Ktq2HH/giphy.gif" alt="Michael Scott from 'The Office' facepalming" width="480" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One day, I asked myself, "Can I write a program that can do this for me while I ... &lt;em&gt;not&lt;/em&gt; do this??"&lt;/p&gt;

&lt;p&gt;Fortunately, the answer was yes.&lt;/p&gt;




&lt;p&gt;There are several ways to safely and quickly delete files in Python.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Using os.remove()
&lt;/h4&gt;

&lt;p&gt;The &lt;a href="https://docs.python.org/3/library/os.html"&gt;OS module&lt;/a&gt; contains a few methods that can help you delete a file (or many files). The &lt;code&gt;os.remove()&lt;/code&gt; function permanently deletes a file from the server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os

os.remove('name_of_file.txt')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's always good practice to check if the file exists before trying to delete it. Otherwise, you'll run into a &lt;code&gt;FileNotFoundError&lt;/code&gt; exception.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os

my_file = 'name_of_file.txt'

## Make sure file exists
if os.path.exists(my_file):
    os.remove(my_file)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Using os.unlink()
&lt;/h4&gt;

&lt;p&gt;Similarly to &lt;code&gt;os.remove()&lt;/code&gt;, &lt;code&gt;os.unlink()&lt;/code&gt; removes one file permanently. Checking that the file exists before trying to delete it is important here, as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os

file_path = '/path_to_my_file'

if os.path.exists(file_path):
    os.unlink(file_path)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that &lt;code&gt;os.unlink()&lt;/code&gt; is only available in Unix-based systems like macOS and Linux. It may not work in Windows systems.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Deleting multiple files at once
&lt;/h4&gt;

&lt;p&gt;If you have more than one file to delete (i.e., a proverbial boatload), Python offers a few options.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;os.listdir()&lt;/code&gt; function retrieves all files and directories within a specified directory. We can then manipulate that list and iterate over each file and delete.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os

file_path = '/path_to_my_file'

for file in os.listdir(file_path):
    if os.path.isfile(os.path.join('file_path', file)):
        os.remove(os.path.join('file_path', file))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python's &lt;code&gt;glob&lt;/code&gt; module is a little more robust, in that it finds files that match a specified pattern. For example, to remove all .png files from a folder that contains several different types of files, we could do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
import glob

file_path = '/path_to_my_file'

pattern_match = '*.png'

## Get a list of files that match our desired pattern
file_paths = glob.glob(os.path.join(file_path, pattern_match))

for file in file_paths:
    if os.path.exists(file):
        os.remove(file)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  4. Deleting directories
&lt;/h4&gt;

&lt;p&gt;You might need to delete directories, in addition to individual files. Python can help with that, too.&lt;/p&gt;

&lt;p&gt;If the directory is empty, the &lt;code&gt;os.rmdir()&lt;/code&gt; function can delete it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os

dir_path = '/path_to_my_dir'

os.rmdir(dir_path)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the directory is not empty when calling the &lt;code&gt;os.rmdir()&lt;/code&gt; function, an &lt;code&gt;OSError&lt;/code&gt; will be raised. So placing the call to delete a directory within a try/except block can help with this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os

dir_path = '/path_to_my_dir'

try:
    os.rmdir(dir_path)
except OSError as error:
    print (error)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the directory you want to delete is not empty, Python's &lt;a href="https://docs.python.org/3/library/shutil.html"&gt;shutil module&lt;/a&gt; works well in this situation.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;shutil.rmtree()&lt;/code&gt; function can delete an empty or non-empty directory and all of its contents. As with the &lt;code&gt;os.rmdir()&lt;/code&gt; function, it's best to place calls to &lt;code&gt;shutil.rmtree()&lt;/code&gt; within try/except blocks, to handle various errors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import shutil

dir_path = '/path_to_my_dir'

try:
    shutil.rmtree(dir_path)
except Exception as error:
    print (error)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
With all of the above mentioned methods, please remember that these files and folders are deleted permanently. So back up your data or move them to a different location if you're not sure whether you will need these files in the future.








&lt;p&gt;Python has saved me hours of time each month, now that I can delete thousands of files while I complete other tasks. Hopefully, my painstaking experience can serve as a reminder to take a close look at your daily tasks and ask yourself which ones could be automated. You might be pleasantly surprised how much time and effort you can spend on more exciting things.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/l0amJzVHIAfl7jMDos/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l0amJzVHIAfl7jMDos/giphy.gif" alt="Michael Scott from 'The Office' dancing happily" width="480" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What tasks have you automated? Do you have a different preferred way to delete lots and lots of files that wasn't mentioned in this article? Let me know in the comments below!&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>#DEVResolutions2024</title>
      <dc:creator>Margaret Teague</dc:creator>
      <pubDate>Thu, 11 Jan 2024 22:19:18 +0000</pubDate>
      <link>https://dev.to/margaretincali/devresolutions2024-1fb1</link>
      <guid>https://dev.to/margaretincali/devresolutions2024-1fb1</guid>
      <description>Header photo by &lt;a href="https://unsplash.com/@markuswinkler?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash"&gt;Markus Winkler&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/black-and-white-typewriter-on-green-textile-LNzuOK1GxRU?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash"&gt;Unsplash&lt;/a&gt;
  

&lt;h2&gt;
  
  
  2024 Goals:
&lt;/h2&gt;

&lt;p&gt;I have three goals I want to achieve this year, and this post is the best way to lay them out.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Start a blog.
&lt;/h3&gt;

&lt;p&gt;I want to focus on learning as much as I can this year, whether it be about All Things Dev, or taking courses to improve my resume. And I believe writing about what I'm learning and how I'm learning, regardless of my skill level, will help me stay on track and keep myself accountable. Also, others may benefit by learning from my mistakes and by offering insight into what I've shared.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Action steps:

&lt;ul&gt;
&lt;li&gt;I will aim to write at least one post per month, either on this site or my personal website (which is currently under construction).&lt;/li&gt;
&lt;li&gt;The topics can be about anything: something work-related, something I'm learning on, my current favorite streaming show ... anything at all.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Uplevel my skills.
&lt;/h3&gt;

&lt;p&gt;I spent a lot of time last year applying for new jobs. And while I didn't receive any offers (I haven't given up yet!), I did learn that my resume could be seriously beefed up. This is where my second goal for 2024 comes into play.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Action steps:

&lt;ul&gt;
&lt;li&gt;I am currently enrolled in several courses on Coursera. I want to complete certifications in mobile development and data analysis this year.&lt;/li&gt;
&lt;li&gt;I am also going to complete my Bachelor of Science degree this year. While I am a firm believer that a college degree is absolutely not necessary to succeed in tech, and attending college is a privilege not afforded to everyone, I am &lt;strong&gt;THIS CLOSE&lt;/strong&gt; to completing the degree. It's just a matter of marking it off my to-do list at this point.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Get a new job!
&lt;/h3&gt;

&lt;p&gt;I've been working as a Web Developer for the last several years and have really loved it. But, I believe I have grown as much as I can within this role and am looking for new opportunities.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Action steps:

&lt;ul&gt;
&lt;li&gt;I need to work on my as-of-this-moment completely nonexistent networking skills. So I plan to focus on this by reaching out via this site, LinkedIn, and even attending my first hackathon at some point this year.&lt;/li&gt;
&lt;li&gt;I would also like to look into working some smaller freelance jobs, either through Fiverr, Upwork, or the like. It would be another great way to network, and my resume will only be helped by the projects I complete along the way.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;What are your goals for this new year? Do you have any tips or tricks you plan on implementing to stick to them? Please feel free to comment below and let me know what you're working on!&lt;/p&gt;

&lt;p&gt;Hey, look at that! I just completed my first goal for this month!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/L17xM7PvLcqJggsCYa/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/L17xM7PvLcqJggsCYa/giphy.gif" alt="Ryan from 'The Office' marking off something from a list" width="500" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>devresolutions2024</category>
    </item>
  </channel>
</rss>
