A file path, or pathname, names a file or directory by giving its location in a filesystem tree. Its spelling depends on the operating system: Linux and macOS use `/` between components and a single `/` root; Windows paths commonly use `\` and drive roots such as `C:\`. section 12.2
An absolute path starts from a root. A relative path is interpreted from the current working directory: `open('myfile', 'r')`, for example, looks for `myfile` there. `.` denotes the current directory and `..` its parent. Changing the working directory with `os.chdir()` differs from constructing a `Path` for another location. section 12.2 section 13.1 section 12.2.6 section 12.2.3
Use Python's path operations instead of manually inserting separators:
- `os.path.join('bin', 'utils', 'disktools')` combines components according to the operating system's conventions. section 12.7
- `Path('bin') / 'utils' / 'disktools'` constructs a path object. `joinpath()` is another way to add components. section 12.2.5
- Inspect a `Path` with `.parts`, `.name`, `.parent`, or `.suffix` when you need its components, final name, parent, or extension. section 12.2.5
a_path = Path('some', 'directory', 'path.jpg')
a_path.name
'path.jpg'
print(a_path.parent)
some/directory
a_path.suffix
'.jpg'
A short path example shows how `name`, `parent`, and `suffix` separate `some/directory/path.jpg` without parsing its string by hand. section 12.2.5
A path identifies a location; check the filesystem before assuming what is there:
- `os.path.exists(path)` tests whether anything exists there; `os.path.isfile(path)` and `os.path.isdir(path)` distinguish files from directories. `Path` provides corresponding methods. section 12.3 section 12.7
- `glob.glob()` finds matching names; `Path.glob()` returns matching path objects. `Path.iterdir()` likewise yields paths rather than a list of name strings. section 12.4 section 12.4.1
Paths also serve as targets for operations: `rename()` can move a file by giving it a path in another directory, while `mkdir()` creates a directory. `Path.mkdir(parents=True)` can create missing intermediate directories. `unlink()` removes a file, not a directory; `rmdir()` removes only an empty directory. section 12.4.1
Listing 20.1 File files_01.py
import datetime
import pathlib
FILE_PATTERN = "*.txt" #1
ARCHIVE = "archive"
def main():
date_string = datetime.date.today().strftime("%Y-%m-%d") #2
cur_path = pathlib.Path(".")
archive_path = cur_path.joinpath(ARCHIVE)
archive_path.mkdir(exist_ok=True) #3
paths = cur_path.glob(FILE_PATTERN)
for path in paths:
new_filename = f"{path.stem}_{date_string}{path.suffix}"
new_path = archive_path.joinpath(
new_filename) #4
path.rename(new_path) #5
if __name__ == '__main__':
main()
A file-archiving script finds matching `.txt` paths, uses each path's stem and suffix to form a dated name, creates the archive directory, and renames each file into it. section 20.2
For file contents, pass a pathname to `open()` to obtain a file object, or use a `Path` object's text and binary read/write methods. The `Path` write methods replace existing content rather than append to it. section 13.1 section 13.5

