Use Python to delete instances of all files created before a certain point in the local directory

  • 2020-06-15 09:41:06
  • OfStack

Because of my work, I need to regularly clean all files under a folder that have been created for more than 1 year, so Today I focused on the operation of Python for local files and folders. Here is a succinct list of the most common os methods, copied as follows:


os.listdir(dirname) List: dirname Directories and files under 
os.getcwd() : Gets the current working directory 
os.curdir: Returns the current directory ( '.')
os.chdir(dirname): Change the working directory to dirname
os.path.isdir(name): judge name Isn't it 1 A directory, name Not just return the directory false
os.path.isfile(name): judge name Isn't it 1 It's not there name Also return false
os.path.exists(name): Determines whether a file or directory exists name
os.path.getsize(name): Gets the file size if name It's directory return 0L
os.path.abspath(name): Get the absolute path 
os.path.normpath(path): specification path String form 
os.path.split(name): Split the file name with the directory (in fact, if you use the directory entirely, it will also be the last 1 Directories are separated as filenames, and it does not determine whether a file or directory exists. 
os.path.splitext(): Separate file name and extension 
os.path.join(path,name): Connect directories with filenames or directories 
os.path.basename(path): Return file name 
os.path.dirname(path): Return file path 
os.remove(dir) #dir Is the folder or file path to delete 
os.rmdir(path) #path The path to the directory to delete. Just to be clear, use os.rmdir The deleted directory must be an empty directory or the function will fail. 
os.path.getmtime(name)  # Gets the modification time of the file 
os.stat(path).st_mtime # Gets the modification time of the file 
os.stat(path).st_ctime # Gets the file modification time 
os.path.getctime(name)# Gets the creation time of the file 

So I followed these methods, and after some hard work, I finally wrote the method of "clean up all files and folders under a certain path". The code is as follows:


import os
dirToBeEmptied = 'D:\_Data\Python\os' # Folders that need to be emptied 
ds = list(os.walk(dirToBeEmptied)) # Get a list of information for all folders 
dsr = ds[::-1] # Reverse the list, starting with the lowest folder 
for d in dsr: # Iterate through the list 
 print(d) # Print out the list items and observe the pattern 
 if d[2] != []: # If there are files under that path 
  for x in d[2]: # Clean up your files first 
   os.remove(os.path.join(d[0], x))
for d in dsr: # Iterate through the list again 
 if d[1] != []: # If there are subfolders under that path 
  for y in d[1]: # Clean up the subfolders 
   os.rmdir(os.path.join(d[0], y))

The reason for the trouble is that os.rmdir () has a problem that only the "empty" folder can be deleted. So you have to start at the bottom of the folder, level 1 level 1 level 1 up to clean.

Then It occurred to me that there might have been an easier way, since emptying folders is a very common action. After checking the official documents of Python, I found another module besides os: shutil (advanced file operation). shutil. rmtree() actually has the method of not only clearing out, but also deleting the folder directly.

To "just empty", I rewrote the code with the shutil module:


import shutil, os
os.chdir('d:\_data\python\os') # Go to the directory you want to empty 
ds = list(os.listdir()) # Gets a list of all files or folders in that directory 
for d in ds: # Iterate through the list 
 if os.path.isfile(d): # If the list item is a file 
  os.remove(d) # Delete directly 
 else: # If it's not a file, it's a folder 
  shutil.rmtree(d) # Also delete directly 

This is a lot cleaner.

However, clearing 1 is great, but it's a diversion from my original purpose: I just want to delete expired files, but folders and all subfolders have to keep ah. Not surprisingly, in the original code, the first traversal of the list was to delete files, not folders. Just add a judgment statement to determine whether the file is out of date, you can achieve the purpose.

The file I'm trying to delete is actually a mirror backup that temporarily saves files that have been deleted or changed from the host for restoration purposes. These files take up more and more space over time, so they are set to expire in 1 year. The one thing about these files is that they will only be copied once and will not change anything, so you just need to determine when they were created.

Thought is easy, but never thought, Python time processing so complex! Two modules are involved: datetime and time. I have time to start with systematic learning 1, but here, I only care about the methods I need now:

datetime. datetime. now() # gets the current time and returns a value similar to datetime. datetime(2017, 12, 14, 1, 29, 24, 406538)

datetime.timedelta() # set the interval between two times for time calculation. The units that can be set include :(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, weeks=0)

os.path.getctime () # gets the creation time of the file and returns the value in the form of a giant floating point number, the number of seconds elapsed between January 1, 1970, and the creation time

datetime.fromtimestamp () # converts os.path.getctime () to a format similar to datetime.datetime.now ()1 for comparison calculation

Once you've sorted out the mess above, you can write the code:


import os, datetime
dirToBeEmptied = 'D:\_Data\Python\os' # Folders that need to be emptied 
ds = list(os.walk(dirToBeEmptied)) # Get a list of information for all folders 
delta = datetime.timedelta(days=365) # set 365 The file dated days ago is expired 
now = datetime.datetime.now() # Get current time 
for d in ds: # Iterate through the list 
 os.chdir(d[0]) # Enter the level path to prevent files from being found and error 
 if d[2] != []: # If there are files under that path 
  for x in d[2]: # Traverse through these files 
   ctime = datetime.datetime.fromtimestamp(os.path.getctime(x)) # Gets the file creation time 
   if ctime < (now-delta): # If established in delta Days ago, 
    os.remove(x) # Then delete the 

Call ~ ~ ~ ~ ~ ~ ~ ~ ~

It's so complicated! I wonder if there's an easy way to...


Related articles: