| title | Python Pathlib Module - Python Cheatsheet |
|---|---|
| description | This module provides a portable way of using operating system dependent functionality. |
This module provides a portable way of using operating system dependent functionality.
Pathlib vs Os Module The pathlib module provides a lot more functionality than the ones listed here, like getting file name, getting file extension, reading/writing a file without manually opening it, etc. See the official documentation if you intend to know more.On Windows, paths are written using backslashes (\) as the separator between
folder names. On Unix based operating system such as macOS, Linux, and BSDs,
the forward slash (/) is used as the path separator. Joining paths can be
a headache if your code needs to work on different platforms.
Fortunately, Python provides us with os.path.join to handle this.
>>> import os
>>> os.path.join('usr', 'bin', 'spam')
# 'usr\\bin\\spam'Joining paths is helpful if you need to create different file paths under the same directory.
>>> my_files = ['accounts.txt', 'details.csv', 'invite.docx']
>>> for filename in my_files:
... print(os.path.join('C:\\Users\\asweigart', filename))
...
# C:\Users\docs\accounts.txt
# C:\Users\docs\details.csv
# C:\Users\docs\invite.docx>>> import os
>>> os.getcwd()
# 'C:\\Python34'
>>> os.chdir('C:\\Windows\\System32')
>>> os.getcwd()
# 'C:\\Windows\\System32'>>> import os
>>> os.makedirs('C:\\delicious\\walnut\\waffles')There are two ways to specify a file path.
- An absolute path, which always begins with the root folder
- A relative path, which is relative to the program’s current working directory
There are also the dot (.) and dot-dot (..) folders. These are not real folders, but special names that can be used in a path. A single period (“dot”) for a folder name is shorthand for “this directory.” Two periods (“dot-dot”) means “the parent folder.”
To see if a path is an absolute path:
>>> import os
>>> os.path.isabs('/')
# True
>>> os.path.isabs('..')
# FalseYou can also extract an absolute path:
>>> import os
>>> os.getcwd()
'/home/docs'
>>> os.path.abspath('..')
'/home'You can get a relative path from a starting path to another path.
>>> import os
>>> os.path.relpath('/etc/passwd', '/')
# 'etc/passwd'>>> import os
>>> os.path.exists('.')
# True
>>> os.path.exists('setup.py')
# True
>>> os.path.exists('/etc')
# True
>>> os.path.exists('nonexistentfile')
# False>>> import os
>>> os.path.isfile('setup.py')
# True
>>> os.path.isfile('/home')
# False
>>> os.path.isfile('nonexistentfile')
# False>>> import os
>>> os.path.isdir('/')
# True
>>> os.path.isdir('setup.py')
# False
>>> os.path.isdir('/spam')
# False>>> import os
>>> os.path.getsize('C:\\Windows\\System32\\calc.exe')
# 776192>>> import os
>>> os.listdir('C:\\Windows\\System32')
# ['0409', '12520437.cpx', '12520850.cpx', '5U877.ax', 'aaclient.dll',
# --snip--
# 'xwtpdui.dll', 'xwtpw32.dll', 'zh-CN', 'zh-HK', 'zh-TW', 'zipfldr.dll']>>> import os
>>> total_size = 0
>>> for filename in os.listdir('C:\\Windows\\System32'):
... total_size = total_size + os.path.getsize(os.path.join('C:\\Windows\\System32', filename))
...
>>> print(total_size)
# 1117846456-
Calling
os.unlink(path)will delete the file at path. -
Calling
os.rmdir(path)will delete the folder at path. This folder must be empty of any files or folders.
>>> import os
>>>
>>> for folder_name, subfolders, filenames in os.walk('C:\\delicious'):
... print(f'The current folder is {folder_name}')
... for subfolder in subfolders:
... print(f'SUBFOLDER OF {folder_name}: {subfolder}')
... for filename in filenames:
... print(f'FILE INSIDE {folder_name}: filename{filename}')
... print('')
...
# The current folder is C:\delicious
# SUBFOLDER OF C:\delicious: cats
# SUBFOLDER OF C:\delicious: walnut
# FILE INSIDE C:\delicious: spam.txt
# The current folder is C:\delicious\cats
# FILE INSIDE C:\delicious\cats: catnames.txt
# FILE INSIDE C:\delicious\cats: zophie.jpg
# The current folder is C:\delicious\walnut
# SUBFOLDER OF C:\delicious\walnut: waffles
# The current folder is C:\delicious\walnut\waffles
# FILE INSIDE C:\delicious\walnut\waffles: butter.txt