finding/counting files

Steven D. Majewski sdm7g at virginia.edu
Fri Jul 28 15:05:27 EDT 2000


On Fri, 28 Jul 2000 jesse at multimediacollective.com wrote:

>    What would the proper command be for finding a file within a 
> folder?  I want my program to check to see if there is a folder 
> called 'test.txt' in the folder, and if there is, count the number of 
> files in that folder.  What commands would I use for both these 
> problems?


Not sure about the references to your pronouns -- do you mean 
something like: 

files = os.listdir(folder)
if "test.txt" in files : print len(files)


or do you mean:
if "test.txt" in files and os.path.isdir(os.path.join(folder,"test.txt")):
	print len(os.listdir(os.path.join(folder,"test.txt")))


If you want something more complicated than that, look at the docs
for os and os.path modules, especially these functions:
	os.listdir 
	os.path.walk
	os.path.join
	os.path.isfile
	os.path.isdir 



-- Steve. 





More information about the Python-list mailing list