Close Menu
    Facebook X (Twitter) Instagram
    Trending
    • SharePoint Internal Column Name Encoded Character List
    • Satchel Pelts List for Red Dead Redemption 2
    • Create a Video Wall With FFmpeg
    • Mitsubishi Outlander Towbar Electrics
    • Mitsubishi Outlander Towbar Installation
    • Telephone Block List
    • Hard Drive Repair After Wrong Voltage Mishap
    • How To Send Excel Workbook As Email Using Button
    YouTube Instagram Facebook RSS
    Technology Spy
    • Tutorials & Help
    • Reviews
    Technology Spy
    You are at:Home»Development»Python»How To List Files In Python Filtered By Age And Size
    Coding Programming Guides

    How To List Files In Python Filtered By Age And Size

    0
    By Matt on September 7, 2013 Python, Tutorials & Help

    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.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow To Combine Text And CSV Files With Python
    Next Article How To Rotate The Characters In A Text String Using Python

    Related Posts

    Create a Video Wall With FFmpeg

    Mitsubishi Outlander Towbar Electrics

    Mitsubishi Outlander Towbar Installation

    Leave A Reply Cancel Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    109

    Recent Posts
    May 1, 2025

    SharePoint Internal Column Name Encoded Character List

    March 24, 2025

    Satchel Pelts List for Red Dead Redemption 2

    January 29, 2025

    Create a Video Wall With FFmpeg

    Categories
    • 3D Printing
    • Android
    • Arduino
    • Development
    • ESP8266
    • Excel
    • Gaming
    • General
    • GIMP
    • Home Automation
    • JavaScript
    • Linux
    • Microsoft Office
    • Mobile Devices
    • Oracle APEX
    • Python
    • Raspberry Pi
    • Reviews
    • Security
    • SharePoint
    • Tutorials & Help
    • VBScript
    Web Tools

    A set of quick and basic online tools for web designers and software developers :

    • Hash Generator
    • Text to HTML List
    • Text to HTML Table
    • URL Encoder and Decoder
    • UNIX Timestamp Calculator
    • LED Resistor Calculator
    • Extract Email from Text
    • Mortgage Calculator
    Tags
    Android APEX Apple Arduino Black Friday Coolermaster csv Elite Dangerous email ESP-01 ESP8266 EV Excel file handling Format gaming GIMP Gritin Home Assistant Home Automation Linux lists Media os.stat os.walk Outlander Power Python Qi RDR2 Review scam Security SharePoint 2010 string Syncwire text text files TrueCrypt Ubuntu USB-C VBA VBscript Windows 10 Xbox One
    About

    Welcome to Technology Spy, a site devoted to gadgets, computers, programming and all things technology! You’ll also find product reviews for items I own as well as tutorials, guides and scripts for the software I use.

    Archives
    Other Resources
    • MattsBits
    • Raspberry Pi Spy
    YouTube Facebook Instagram Pinterest RSS

    Entries RSS | Comments RSS

    Copyright © 2025 - All Rights Reserved - Matt Hawkins

    mastodon.social@MattHawkins

    Type above and press Enter to search. Press Esc to cancel.