List directory contents in CSV file using Python
Do you need to compile a list of all of the files in a folder or directory? This can be automated using a simple Python script which will save a list of the files (and their full paths) into a CSV file that can be read in Excel.
I frequently need to compile a list of files that exist in a directory or folder. Whilst there are other methods, this one uses Python which I like because it works across operating systems.
There are three parts:
The Code
Here is the Python code, which I call listfiles.py:
#!/usr/bin/env python
import os
import csv
import sys
import csv
from datetime import datetime
import argparse
import glob
# Intro Text
print("\033[1;34;40m\n\nCSV listing of files \033[0m")
print("\033[1;36;40mWritten by Adam Dimech. \n \n \n \033[0m")
def options():
parser = argparse.ArgumentParser(description="Create a list of images from a folder in a CSV file")
parser.add_argument("-f", "--folder", help="Target folder of images.", required=True)
parser.add_argument("-r", "--recurse", help="Search recursively.", default=False, action="store_true")
args = parser.parse_args()
return args
def main():
# Get options
args = options()
# Identify the target directory
target_raw = args.folder
# Clean up Windows file paths
if sys.platform.startswith('win'):
target_raw = target_raw.replace('\\', '/')
if not target_raw.endswith('/'):
target = target_raw+"/"
else:
target = os.path.join(target_raw, '') # Add trailing slash if missing
print("The target directory is", target, '\nScanning folder for files...')
# Generate Time Stamp and generate CSV name with timestamp
thedate = datetime.now()
filedatestring = thedate.strftime("%Y%m%d%H%M%S%f")
csv_file_name = "File-List-"+filedatestring+".csv"
csv_file = target+csv_file_name
fieldnames = ['filepath']
# Create CSV file
with open(csv_file, 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
# Check if search is to be recursive
if args.recurse is True:
state = True
desc = '**/*.*'
else:
state = False
desc = '*.*'
# Iterate through folder and list files
for f in glob.glob(target+desc, recursive=state):
if sys.platform.startswith('win'): # Forward slashes in Windows
f = f.replace("\\", "/")
with open(csv_file, 'a', newline='') as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writerow({'filepath': f})
# Statement
print("\033[1;32;40mComplete! \033[0m Your CSV was saved to", csv_file)
if __name__ == '__main__':
main()
Commands
To execute this command, you need to activate your Python environment and then enter three variables on the command line:
- The full path to listfiles.py
-f
: The directory that you want to search (required)-r
: A flag to indicate whether the search should be recursive (optional, default is no recursive search)
So, a recursive search of c:\users\myname\Downloads
would be entered as follows (note the -r flag):
c:\path\to\listfiles.py -f c:\users\myname\Downloads -r
A non-recursive search would be as follows:
c:\path\to\listfiles.py -f c:\users\myname\Downloads
Output
A CSV file containing a list of files and their paths will be saved into the target directory.
Download the Code
The code is available on GitHub Gist.
Comments
No comments have yet been submitted. Be the first!