15
Lesson 29 of 44

File system

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.

  • 5 theory articles
  • 10 practice exercises
Start lesson

Fundamentals concepts 5

Paths as objects: pathlib

When your programs handle real files, writing paths as text (“ folder/ + name ”) is a source of errors: different separators…

Theory
Read more

Walking folders and searching for files

There are three usual ways to walk a folder: iterdir() (first level only), glob / rglob (with patterns) and os.walk (the whole…

Theory
Read more

Create, copy, move and delete

Path methods cover the basic operations and the shutil module (*shell utilities*) the higher-level ones: copying or deleting…

Theory
Read more

File information and duplicates

stat() returns data about a file: size ( st_size , in bytes), modification date ( st_mtime , in seconds since 1970)… And to…

Theory
Read more

Errors and good practices with paths

Problems with the file system are almost never in the code but in the environment : the file is not there, the folder already…

Theory
Read more

Practice exercises 10

List a folder

The program runs inside a folder that already contains the files of the test case. With Path.iterdir() you walk through the…

Exercise Easy
Solve it

Count files by extension

The program runs inside a folder that already contains the files of the test case. With Path.rglob("*") you walk through the…

Exercise Medium
Solve it

Size of a folder

The program runs inside a folder that already contains the files of the test case. path.stat() returns information about a file…

Exercise Medium
Solve it

Directory tree

The program runs inside a folder that already contains the files of the test case. A recursive function that walks the folder…

Exercise Hard
Solve it

Search with patterns

The program runs inside a folder that already contains the files of the test case. Path.glob(pattern) searches for files and…

Exercise Medium
Solve it

Name, extension and folder

pathlib does not need the file to exist: paths are objects that can be analyzed. PurePosixPath works only with the text of the…

Exercise Easy
Solve it

Organize by extension

The program runs inside a folder that already contains the files of the test case. mkdir(exist_ok=True) creates a folder and…

Exercise Hard
Solve it

Backup copy

The program runs inside a folder that already contains the files of the test case. shutil.copytree(source, target) copies a whole…

Exercise Medium
Solve it

Duplicate files

The program runs inside a folder that already contains the files of the test case. Two files are duplicates if they have exactly…

Exercise Hard
Solve it

Batch rename

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…

Exercise Medium
Solve it