How do you get the filenames in a directory? You can do that automatically with Python, including all files in sub directories.
To get started, you need to import the os module, this contains operating system specific functionality.
In Python, you can use OS.walk(). OS.walk() gets the filenames in a directory. It does so by walking the tree either top-down or bottom-up.
You should know the basics of Python or this will be a bit confusing.
Calling the OS.walk() method returns a 3-tuple (dirpath, dirnames, filenames).
os.walk(top, topdown=True, onerror=None, followlinks=False)
- dirpath is a string, the path to the directory.
- topdown is an optional argument
- onerror is an optional argument, by default errors are ignored
- followlinks is false will not walk down into symbolic links that resolve to directories
The function returns lists for dirnames and filesnames, so you can use a for loop to parse it.
To output the files in a directory you can run the code below:
import os for root, dirs, files in os.walk("/etc/"): for name in files: print( root + "/" + name)
This outputs all files in the directory, including sub-directories. To get all sub directories you can just change files to dirs:
import os for root, dirs, files in os.walk("/etc/"): for name in dirs: print( root + "/" + name)
You can get the file size too:
import os from os.path import join, getsize from pathlib import Path # needs Python 3.5+ home = str(Path.home()) for root, dirs, files in os.walk(home + "/Desktop/"): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files")
I've used this line to get the home directory in Python 3.5:
from pathlib import Path home = str(Path.home())
If you have an older Python version you can use
from os.path import expanduser home = expanduser("~")