-
-
Notifications
You must be signed in to change notification settings - Fork 597
Expand file tree
/
Copy pathfile_handling.py
More file actions
121 lines (87 loc) · 3.75 KB
/
file_handling.py
File metadata and controls
121 lines (87 loc) · 3.75 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
File handling is a fundamental concept in Python that involves
opening, reading, writing, and appending to files. This module
demonstrates both traditional and modern approaches using pathlib.
Traditional approach: The builtin 'open' function works with string
paths and different modes like reading ('r'), writing ('w'), and
appending ('a').
Modern approach: pathlib.Path provides an object-oriented interface
for working with filesystem paths, offering cleaner syntax and
cross-platform support. pathlib is preferred for new code as it
replaces older os.path operations with an intuitive API.
"""
import os
from pathlib import Path
_TARGET_FILE = "sample.txt"
def read_file(filename: str) -> str:
"""Read content from existing file."""
with open(filename, "r") as file:
content = file.read()
return content
def write_file(filename: str, content: str) -> str:
"""Write content to new file."""
with open(filename, "w") as file:
file.write(content)
return f"Content written to '{filename}'."
def append_file(filename: str, content: str) -> str:
"""Append content to existing file."""
with open(filename, "a") as file:
file.write(content)
return f"Content appended to '{filename}'."
def delete_file(filename: str) -> str:
"""Delete content of existing file."""
os.remove(filename)
return f"'{filename}' has been deleted."
def write_file_pathlib(filename: str, content: str) -> str:
"""Write content using pathlib.Path."""
path = Path(filename)
path.write_text(content)
return f"Content written to '{filename}' using pathlib."
def read_file_pathlib(filename: str) -> str:
"""Read content using pathlib.Path."""
path = Path(filename)
return path.read_text()
def append_file_pathlib(filename: str, content: str) -> str:
"""Append content using pathlib.Path."""
path = Path(filename)
current = path.read_text()
path.write_text(current + content)
return f"Content appended to '{filename}' using pathlib."
def delete_file_pathlib(filename: str) -> str:
"""Delete file using pathlib.Path."""
path = Path(filename)
path.unlink()
return f"'{filename}' has been deleted using pathlib."
def main() -> None:
# Test traditional file operations
result = write_file(_TARGET_FILE, "This is a test.")
assert result == f"Content written to '{_TARGET_FILE}'."
content = read_file(_TARGET_FILE)
assert content == "This is a test."
append_result = append_file(_TARGET_FILE, "\nThis is an appended line.")
assert append_result == f"Content appended to '{_TARGET_FILE}'."
content = read_file(_TARGET_FILE)
assert content == "This is a test.\nThis is an appended line."
delete_result = delete_file(_TARGET_FILE)
assert delete_result == f"'{_TARGET_FILE}' has been deleted."
# Test pathlib operations
pathlib_file = "pathlib_sample.txt"
result = write_file_pathlib(pathlib_file, "Pathlib is modern.")
assert result == f"Content written to '{pathlib_file}' using pathlib."
content = read_file_pathlib(pathlib_file)
assert content == "Pathlib is modern."
append_result = append_file_pathlib(pathlib_file, "\nIt's object-oriented.")
assert append_result == f"Content appended to '{pathlib_file}' using pathlib."
content = read_file_pathlib(pathlib_file)
assert content == "Pathlib is modern.\nIt's object-oriented."
# Test path operations with pathlib
path = Path(pathlib_file)
assert path.exists()
assert path.is_file()
assert path.suffix == ".txt"
assert path.stem == "pathlib_sample"
delete_result = delete_file_pathlib(pathlib_file)
assert delete_result == f"'{pathlib_file}' has been deleted using pathlib."
assert not path.exists()
if __name__ == "__main__":
main()