December 2018
Beginner to intermediate
796 pages
19h 54m
English
With Python, you can also inspect the content of a directory. I'll show you two ways of doing this:
# files/listing.pyimport oswith os.scandir('.') as it: for entry in it: print( entry.name, entry.path, 'File' if entry.is_file() else 'Folder' )
This snippet uses os.scandir, called on the current directory. We iterate on the results, each of which is an instance of os.DirEntry, a nice class that exposes useful properties and methods. In the code, we access a subset of those: name, path, and is_file(). Running the code yields the following (I omitted a few results for brevity):
$ python listing.pyfixed_amount.py ./fixed_amount.py Fileexistence.py ./existence.py File...ops_example ./ops_example Folder...
A more powerful way ...
Read now
Unlock full access