Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python to execute a command on every file in a folder

I'm trying to create a Python script that would :

  1. Look into the folder "/input"
  2. For each video in that folder, run a mencoder command (to transcode them to something playable on my phone)
  3. Once mencoder has finished his run, delete the original video.

That doesn't seem too hard, but I suck at python :)

Any ideas on what the script should look like ?

Bonus question : Should I use

os.system

or

subprocess.call

?

Subprocess.call seems to allow for a more readable script, since I can write the command like this :

cmdLine = ['mencoder', sourceVideo, '-ovc', 'copy', '-oac', 'copy', '-ss', '00:02:54', '-endpos', '00:00:54', '-o', destinationVideo]

EDIT : Ok, that works :

import os, subprocess  bitrate = '100' mencoder = 'C:\\Program Files\\_utilitaires\\MPlayer-1.0rc2\\mencoder.exe' inputdir = 'C:\\Documents and Settings\\Administrator\\Desktop\\input' outputdir = 'C:\\Documents and Settings\\Administrator\\Desktop\\output'  for fichier in os.listdir(inputdir):     print 'fichier :' + fichier     sourceVideo = inputdir + '\\' + fichier     destinationVideo = outputdir + '\\' + fichier[:-4] + ".mp4"      commande = [mencoder,                '-of',                'lavf',                [...]                '-mc',                '0',                 sourceVideo,                '-o',                destinationVideo]      subprocess.call(commande)  os.remove(sourceVideo) raw_input('Press Enter to exit') 

I've removed the mencoder command, for clarity and because I'm still working on it.

Thanks to everyone for your input.

like image 620
Manu Avatar asked Jul 13 '09 16:07

Manu


People also ask

Can you iterate through a folder in Python?

Python as a scripting language provides various methods to iterate over files in a directory. This function returns the list of files and subdirectories present in the given directory. We can filter the list to get only the files using os. path.

How do I loop multiple files in a directory in Python?

Import the os library and pass the directory in the os. listdir() function. Create a tuple having the extensions that you want to fetch. Through a loop iterate over all the files in the directory and print the file having a particular extension.


2 Answers

To find all the filenames use os.listdir().

Then you loop over the filenames. Like so:

import os for filename in os.listdir('dirname'):      callthecommandhere(blablahbla, filename, foo) 

If you prefer subprocess, use subprocess. :-)

like image 101
Lennart Regebro Avatar answered Oct 11 '22 09:10

Lennart Regebro


Use os.walk to iterate recursively over directory content:

import os  root_dir = '.'  for directory, subdirectories, files in os.walk(root_dir):     for file in files:         print os.path.join(directory, file) 

No real difference between os.system and subprocess.call here - unless you have to deal with strangely named files (filenames including spaces, quotation marks and so on). If this is the case, subprocess.call is definitely better, because you don't need to do any shell-quoting on file names. os.system is better when you need to accept any valid shell command, e.g. received from user in the configuration file.

like image 40
Maciej Pasternacki Avatar answered Oct 11 '22 07:10

Maciej Pasternacki