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)?
To get a list of all the files and folders in a particular directory in the filesystem, use os. listdir() in legacy versions of Python or os. scandir() in Python 3.
1st element of os.listdir()
import os
os.listdir('/foo/bar')[0]
Well I know this code works...
for file in os.listdir('.'):
#do something
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With