Skip to content Skip to sidebar Skip to footer

Running A Python Script On All The Files In A Directory

I have a Python script that reads through a text csv file and creates a playlist file. However I can only do one at a time, like: python playlist.py foo.csv foolist.txt However, I

Solution 1:

for f in *.csv; do
  python playlist.py "$f""${f%.csv}list.txt"done

Will that do the trick? This will put foo.csv in foolist.txt and abc.csv in abclist.txt.

Or do you want them all in the same file?

Solution 2:

Just use a for loop with the asterisk glob, making sure you quote things appropriately for spaces in filenames

for file in *.csv; do
   python playlist.py "$file" >> outputfile.txt;
done

Solution 3:

if you have directory name you can use os.listdir

os.listdir(dirname)

if you want to select only a certain type of file, e.g., only csv file you could use glob module.

Post a Comment for "Running A Python Script On All The Files In A Directory"