From the course: Python: Working with Files

Unlock the full course today

Join today to access over 24,000 courses taught by industry experts.

Understand Python's new pathlib module

Understand Python's new pathlib module - Python Tutorial

From the course: Python: Working with Files

Understand Python's new pathlib module

- [Instructor] Traditionally, Python has represented file paths using regular text strings. Paths have lots of information and in order to retrieve that information, important functionality is spread all around the standard library, including in the OS, glob and shutil modules. The pathlib module was introduced in Python 3.4 and it presents an alternative way of representing directory and file paths. It represents them as objects instead of strings. To use it, we can import path from the pathlib module. Let's print this directory's contents. To access the current directory, we can use the cwd function on the Path module. This object is either a POSIX path or a Windows path object, depending on the operating system. With the entries, we can iterate through each entry and print out specific attributes. Entries.iterdir creates an iterator of all the files and folders in the current working directory. Each entry contains…

Contents