A directory organizes files and other directories into a tree. A pathname identifies a location by naming the directories along the route to it. Path syntax differs across operating systems: UNIX-style paths use / and have one root; Windows paths traditionally use \ and have drive roots. Use Python’s path-handling tools rather than assuming one spelling works everywhere. section 12.2
Relative paths depend on the current working directory, which need not be the directory containing the program. Use os.getcwd() to inspect it, os.chdir() to change it, and os.listdir(os.curdir) to list its entries. section 12.2.2
Common directory operations have different scopes and safeguards:
- os.listdir lists entries in one directory; glob.glob matches pathnames using wildcards. section 12.4
- os.mkdir creates one directory; os.makedirs can also create missing intermediate directories. section 12.4
- os.rename can move a file or directory. os.rmdir removes only an empty directory; shutil.rmtree recursively removes a nonempty tree. section 12.4
To process an entire subtree, os.walk(start) yields a path, its subdirectory names, and its file names for each directory. It walks top-down by default; bottom-up traversal and following symbolic-link directories are optional. During top-down traversal, remove names from the subdirectory list to skip those branches. section 12.5
import os
for root, dirs, files in os.walk(os.curdir):
print("{0} has {1} files".format(root, len(files)))
if ".config" in dirs: #1
dirs.remove(".config") #2
This loop counts files throughout a tree but removes .config from the directories that os.walk will visit next. section 12.5
Directory layout can express relationships. An archive can keep every received set of files in its own date-named subdirectory, rather than relying on dates added to individual filenames. Create the destination directory, then move each file into it. section 20.2 section 20.3
Listing 20.2 File files_02.py
import datetime
import pathlib
FILE_PATTERN = "*.txt"
ARCHIVE = "archive"
def main():
date_string = datetime.date.today().strftime("%Y-%m-%d")
cur_path = pathlib.Path(".")
archive_path = cur_path.joinpath(ARCHIVE)
archive_path.mkdir(exist_ok=True) #1
new_path = archive_path.joinpath(date_string)
new_path.mkdir(exist_ok=True) #2
paths = cur_path.glob(FILE_PATTERN)
for path in paths:
path.rename(new_path.joinpath(path.name))
if __name__ == '__main__':
main()
The pathlib example creates the archive and date directories when needed, then uses each file’s path to move it into the dated group. section 20.3
Python packages also map directory structure to module names, allowing related code to be split across files. Keep that hierarchy as flat as practical: most packages need only one top-level directory, and few need more than two levels. section 18.2 section 18.5






