-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path32_Pathlib_module.py
More file actions
31 lines (25 loc) · 828 Bytes
/
32_Pathlib_module.py
File metadata and controls
31 lines (25 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# pathlib module -> Object-oriented filesystem paths
# Listing Subdirectories
from pathlib import Path
# p = Path('/')
# for subdir in p.iterdir():
# print(subdir)
# Listing Python Source Files in This Directory Tree
p = Path('/')
# files = p.rglob('*.py')
# for f in files:
# print(f)
# Querying Path Properties
print(("Is absolute?", p.is_absolute()))
print(("Is a file?", p.is_file()))
print(("Is a directory?", p.is_dir()))
print(("File name:", p.name))
print(("File suffix:", p.suffix))
print(("File stem:", p.stem))
print(("Parent directory:", p.parent))
# Reading and writing to a file
with (p / 'sample.txt').open('r', encoding='utf-8') as file:
content = file.read()
print(content)
with (p / 'sample.txt').open('a',encoding='utf-8') as file:
file.write("\nAppended line using pathlib module.")