PWP Notes 6
PWP Notes 6
6.2 File Handling: Opening file in different modes, accessing file contents
using standard library functions, Reading and writing files, closing a file,
Renaming and deleting files, Directories in Python, File and directory related
standard functions
In Python, the standard library functions are used to access file contents efficiently
Directories in Python
In Python, directories can be created, accessed, and managed using the os and os.path
modules.
# Open the first file in read mode and read its content
with open('first.txt', 'r') as first_file:
content = first_file.read()
# Open the second file in write mode and write the content to it
with open('second.txt', 'w') as second_file:
second_file.write(content)
● Exceptions are errors that occur during program execution, such as dividing by zero or
accessing a file that doesn’t exist.
● If exceptions are not handled, the program terminates
2. Exception Handling Syntax
try-except Block