Paths as objects: pathlib
When your programs handle real files, writing paths as text (“ folder/ + name ”) is a source of errors: different separators…
Besides reading and writing files, a program needs to move around folders: list, search, copy, move, rename and delete. In this lesson you will use pathlib to handle paths as objects, walk folder trees with glob and os.walk, work with shutil, compute sizes and content fingerprints to find duplicates, and learn to anticipate the typical errors of the environment. The exercises run in a folder of their own that contains the files of each test.
When your programs handle real files, writing paths as text (“ folder/ + name ”) is a source of errors: different separators…
There are three usual ways to walk a folder: iterdir() (first level only), glob / rglob (with patterns) and os.walk (the whole…
Path methods cover the basic operations and the shutil module (*shell utilities*) the higher-level ones: copying or deleting…
stat() returns data about a file: size ( st_size , in bytes), modification date ( st_mtime , in seconds since 1970)… And to…
Problems with the file system are almost never in the code but in the environment : the file is not there, the folder already…
The program runs inside a folder that already contains the files of the test case. With Path.iterdir() you walk through the…
The program runs inside a folder that already contains the files of the test case. With Path.rglob("*") you walk through the…
The program runs inside a folder that already contains the files of the test case. path.stat() returns information about a file…
The program runs inside a folder that already contains the files of the test case. A recursive function that walks the folder…
The program runs inside a folder that already contains the files of the test case. Path.glob(pattern) searches for files and…
pathlib does not need the file to exist: paths are objects that can be analyzed. PurePosixPath works only with the text of the…
The program runs inside a folder that already contains the files of the test case. mkdir(exist_ok=True) creates a folder and…
The program runs inside a folder that already contains the files of the test case. shutil.copytree(source, target) copies a whole…
The program runs inside a folder that already contains the files of the test case. Two files are duplicates if they have exactly…
The program runs inside a folder that already contains the files of the test case. path.with_name(new) changes the name of a file…