11"""
22File handling is a fundamental concept in Python that involves
33opening, reading, writing, and appending to files. This module
4- demonstrates the basics of file handling in Python .
4+ demonstrates both traditional and modern approaches using pathlib .
55
6- Python provides various ways to work with files. We can use the
7- builtin 'open' function to open files in different modes like
8- reading ('r'), writing ('w'), and appending ('a').
6+ Traditional approach: The builtin 'open' function works with string
7+ paths and different modes like reading ('r'), writing ('w'), and
8+ appending ('a').
9+
10+ Modern approach: pathlib.Path provides an object-oriented interface
11+ for working with filesystem paths, offering cleaner syntax and
12+ cross-platform support. pathlib is preferred for new code as it
13+ replaces older os.path operations with an intuitive API.
914"""
1015
1116import os
17+ from pathlib import Path
1218
1319_TARGET_FILE = "sample.txt"
20+ _TARGET_PATH = Path (_TARGET_FILE )
1421
1522
1623def read_file (filename : str ) -> str :
@@ -40,7 +47,36 @@ def delete_file(filename: str) -> str:
4047 return f"'{ filename } ' has been deleted."
4148
4249
50+ def write_file_pathlib (filename : str , content : str ) -> str :
51+ """Write content using pathlib.Path."""
52+ path = Path (filename )
53+ path .write_text (content )
54+ return f"Content written to '{ filename } ' using pathlib."
55+
56+
57+ def read_file_pathlib (filename : str ) -> str :
58+ """Read content using pathlib.Path."""
59+ path = Path (filename )
60+ return path .read_text ()
61+
62+
63+ def append_file_pathlib (filename : str , content : str ) -> str :
64+ """Append content using pathlib.Path."""
65+ path = Path (filename )
66+ current = path .read_text ()
67+ path .write_text (current + content )
68+ return f"Content appended to '{ filename } ' using pathlib."
69+
70+
71+ def delete_file_pathlib (filename : str ) -> str :
72+ """Delete file using pathlib.Path."""
73+ path = Path (filename )
74+ path .unlink ()
75+ return f"'{ filename } ' has been deleted using pathlib."
76+
77+
4378def main () -> None :
79+ # Test traditional file operations
4480 result = write_file (_TARGET_FILE , "This is a test." )
4581 assert result == f"Content written to '{ _TARGET_FILE } '."
4682
@@ -56,6 +92,31 @@ def main() -> None:
5692 delete_result = delete_file (_TARGET_FILE )
5793 assert delete_result == f"'{ _TARGET_FILE } ' has been deleted."
5894
95+ # Test pathlib operations
96+ pathlib_file = "pathlib_sample.txt"
97+ result = write_file_pathlib (pathlib_file , "Pathlib is modern." )
98+ assert result == f"Content written to '{ pathlib_file } ' using pathlib."
99+
100+ content = read_file_pathlib (pathlib_file )
101+ assert content == "Pathlib is modern."
102+
103+ append_result = append_file_pathlib (pathlib_file , "\n It's object-oriented." )
104+ assert append_result == f"Content appended to '{ pathlib_file } ' using pathlib."
105+
106+ content = read_file_pathlib (pathlib_file )
107+ assert content == "Pathlib is modern.\n It's object-oriented."
108+
109+ # Test path operations with pathlib
110+ path = Path (pathlib_file )
111+ assert path .exists ()
112+ assert path .is_file ()
113+ assert path .suffix == ".txt"
114+ assert path .stem == "pathlib_sample"
115+
116+ delete_result = delete_file_pathlib (pathlib_file )
117+ assert delete_result == f"'{ pathlib_file } ' has been deleted using pathlib."
118+ assert not path .exists ()
119+
59120
60121if __name__ == "__main__" :
61122 main ()
0 commit comments