Resources | Subject Notes | Computer Science
File processing is a fundamental task in computer science, allowing programs to read data from and write data to persistent storage. This section explores the core concepts of file operations, including opening, reading, writing, and closing files. It also covers the crucial aspect of exception handling to gracefully manage potential errors during file operations.
When opening a file, we specify a mode that determines how the file will be accessed. Common file modes include:
The following are the fundamental file operations:
open()
function with the appropriate mode.read()
, readline()
, or iterating through the file object.write()
or writelines()
.close()
function to release system resources. It's crucial to close files after use.The following Python code demonstrates basic file processing operations:
def process_file(filename): try: file = open(filename, 'r') contents = file.read() print(\"File Contents:\", contents) file.close() except FileNotFoundError: print(f\"Error: File '{filename}' not found.\") except Exception as e: print(f\"An error occurred: {e}\")process_file(\"my_file.txt\")
Exception handling is essential for robust file processing. File operations can fail due to various reasons, such as the file not existing, insufficient permissions, or disk errors. try
and except
blocks are used to handle these exceptions.
Common exceptions encountered during file operations include:
FileNotFoundError
: Raised when the specified file does not exist.PermissionError
: Raised when the program does not have the necessary permissions to access the file.IOError
: A general exception for input/output errors.The following Python code demonstrates exception handling during file operations:
def read_file_safe(filename): try: file = open(filename, 'r') data = file.read() file.close() return data except FileNotFoundError: print(f\"Error: File '{filename}' not found.\") return None except PermissionError: print(f\"Error: Permission denied to access '{filename}'.\") return None except Exception as e: print(f\"An unexpected error occurred: {e}\") return Nonefile_content = read_file_safe(\"data.txt\")if file_content: print(\"File content read successfully:\", file_content)
Mode | Description | Behavior if file exists | Behavior if file does not exist |
---|---|---|---|
'r' | Read | File must exist | Error |
'w' | Write | Contents are overwritten | New file is created |
'a' | Append | New data is added to the end | New file is created |
'x' | Exclusive creation | Error | New file is created |
'r+' | Read and Write | File must exist | Error |
'w+' | Read and Write | Contents are truncated | New file is created |
'a+' | Read and Append | New data is added to the end | New file is created |
In real-world applications, file processing often involves handling large files efficiently, using buffered I/O, and implementing robust error handling mechanisms. Understanding file modes and exception handling is crucial for writing reliable and robust file processing programs.