[Tutor] how to extract number of files from directory

Alan Gauld alan.gauld at freenet.co.uk
Wed Oct 12 22:01:48 CEST 2005


> a = os.system('ls *JPG | wc -l')
> when i do:
> print a
> i get '0'.

os.system only returns the exitcode of the command not the output.

Look at the new Subprocess  module and its Popen class. It can 
look intimidating at first but read the examples and you should find 
it easy enough.

Altrernatively try the commands module although subprocess is 
intended to replace it...

However you might find it easier to use the python os tools like listdir 
to do the job directly. 

a = len(os.listdir(mydir, '*JPG')

Should be about right...

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list