tags:

views:

131

answers:

3

Given a local directory structure of /foo/bar, and assuming that a given path contains exactly one file (filename and content does not matter), what is a reasonably fast way to get the filename of that single file (NOT the file content)?

+7  A: 

1st element of os.listdir()

import os
os.listdir('/foo/bar')[0]
gimel
Thanks, that does the job!
prometheus
heh... I should have actually thought about it for a second :-p
piggles
+1  A: 

Well I know this code works...

for file in os.listdir('.'):
    #do something
piggles
Thanks, gimel made it even shorter.
prometheus
A: 

you can also use glob

import glob
print glob.glob("/path/*")[0]
ghostdog74