The following post shows you various examples of listing files in Python. You can easily include or exclude files based on size and age.
The example use the os.walk function which grabs filename from a directory structure. The file age and size are extracted using the os.stat function.
Using these functions should allow your code to work across different operating systems as the Python os module deals with the subtle differences in the way these systems handle files.
List all files in a directory
The following script lists all the files in a specified directory. In this example the directory is “D:\temp” :
#!/usr/bin/env python # Import modules import os # Define variables path = r'D:\temp' # List all files print "List all files in " + path print "==================" + "=" * len(path) for root, dirs, files in os.walk(path): for name in files: filename = os.path.join(root, name) print(filename)
You’ll notice there is an ‘r’ character in the path variable. This is not a mistake. The r tells Python this a raw string and to not treat the ‘\’ character as a special character. The print command is used to output the file names but you could perform other actions on the file name depending on the purpose of your script.
List all files older than a number of days
The following script can be used to list all files within a directory and its sub-directories based on their age. Files that are newer are not part of the output.
#!/usr/bin/env python # Import modules import os import time # Define variables xdays = 120 path = r'D:\temp' now = time.time() # List all files older than xdays print "\nList all files older than " + str(xdays) + " days" print "==========================" + "=" * len(str(xdays)) + "=====" for root, dirs, files in os.walk(path): for name in files: filename = os.path.join(root, name) if os.stat(filename).st_mtime < now - (xdays * 86400): print(filename)
List all files newer than a number of days
The following script can be used to list all files within a directory and its sub-directories based on their age. Files newer than the specified number of days are included in the output :
#!/usr/bin/env python # Import modules import os import time # Define variables xdays = 120 path = r'D:\temp' now = time.time() # List all files newer than xdays print "\nList all files newer than " + str(xdays) + " days" print "==========================" + "=" * len(str(xdays)) + "=====" for root, dirs, files in os.walk(path): for name in files: filename = os.path.join(root, name) if os.stat(filename).st_mtime > now - (xdays * 86400): print(filename)
List all files larger than a specified number of bytes
The following script can be used to list all files within a directory and its sub-directories based on their size. Files bigger than the specified number of bytes are included in the output :
#!/usr/bin/env python # Import modules import os # Define variables xsize = 1000000 path = r'D:\temp' # List all files bigger than 1000000 bytes print "\nList files bigger than " + str(xsize) + " bytes" print "=======================" + "=" * len(str(xsize)) + "=====" for root, dirs, files in os.walk(path): for name in files: filename = os.path.join(root, name) if os.stat(filename).st_size > xsize: print(filename)
List all files smaller than a specified number of bytes
The following script can be used to list all files within a directory and its sub-directories based on their size. Files smaller than the specified number of bytes are included in the output :
#!/usr/bin/env python # Import modules import os # Define variables xsize = 1000000 path = r'D:\temp' # List all files bigger than 1000000 bytes print "\nList files smaller than " + str(xsize) + " bytes" print "========================" + "=" * len(str(xsize)) + "=====" for root, dirs, files in os.walk(path): for name in files: filename = os.path.join(root, name) if os.stat(filename).st_size < xsize: print(filename)
Deleting files using the above examples
If you want to delete files based on the code above you can simply replace “print(filename)” with “os.remove(filename)”. Be careful! Make sure you are correctly filtering the files with the print command before you include the os.remove command.