Python offers a straightforward way to interact with files using its built-in functions. The core functionality revolves around the open()
function, which takes the file path and a mode as arguments. Let's explore reading and writing:
Reading Files:
To read a file, you typically use the open()
function with the 'r' mode (read mode) and then use methods like read()
, readline()
, or readlines()
to access the file's content.
# Open a file for reading try: with open("my_file.txt", "r") as file: # Read the entire file content at once contents = file.read() print(contents) # Read the file line by line file.seek(0) # Reset the file pointer to the beginning for line in file: print(line, end="") # end="" prevents extra newline # Read all lines into a list file.seek(0) lines = file.readlines() print(lines) except FileNotFoundError: print("File not found.") except Exception as e: print(f"An error occurred: {e}")
This example demonstrates three ways to read: read()
reads everything at once, iterating with for line in file
reads line by line, and readlines()
reads all lines into a list. The with open(...) as file:
construct ensures the file is automatically closed even if errors occur.
Writing Files:
Writing to a file involves opening it in 'w' (write) mode, 'a' (append) mode, or 'x' (exclusive creation) mode. The write()
method adds content to the file.
try: with open("my_new_file.txt", "w") as file: file.write("This is the first line.\n") file.write("This is the second line.\n") with open("my_new_file.txt", "a") as file: #Append mode file.write("This line is appended.\n") except Exception as e: print(f"An error occurred: {e}")
'w' overwrites existing content, 'a' adds to the end, and 'x' creates a new file and fails if one already exists. Remember to include newline characters (\n
) for proper line breaks.
Python's open()
function supports several file access modes, each dictating how the file is handled:
These modes can be combined. For instance, "r b"
opens a file for both reading and writing in binary mode.
File I/O operations can encounter various errors, such as the file not existing, insufficient permissions, or disk space issues. Robust code should handle these gracefully. The most common approach is using try-except
blocks:
try: with open("my_file.txt", "r") as file: # ... file operations ... except FileNotFoundError: print("File not found. Creating a new file...") with open("my_file.txt", "w") as file: file.write("File created.") except PermissionError: print("Permission denied. Check file permissions.") except OSError as e: print(f"An operating system error occurred: {e}") except Exception as e: #catch any other exception print(f"An unexpected error occurred: {e}")
This example catches specific exceptions (FileNotFoundError
, PermissionError
, OSError
) for more informative error handling and a generic Exception
to catch any other potential issues. Always be specific when possible to handle errors effectively.
Reading and writing large files requires optimization to avoid memory issues and improve performance. Here are some best practices:
for
loop as shown in the first example. This is significantly more memory-efficient for large files.io.BufferedReader
and io.BufferedWriter
classes provide buffered I/O, which significantly improves performance by reducing the number of disk accesses.mmap
(memory mapping). This maps a portion of the file to memory, allowing efficient access to specific parts without loading the whole file.import io import mmap #Chunking Example chunk_size = 1024 with open("large_file.txt", "r") as file: while True: chunk = file.read(chunk_size) if not chunk: break #Process the chunk #... #Memory Mapping Example with open("large_file.txt", "r b") as f: mm = mmap.mmap(f.fileno(), 0) #0 means map the entire file #Access specific parts of the file using mm[start:end] mm.close()
Choosing the best approach depends on the specific application and how the file is accessed. For sequential processing, iterating line by line or using buffered I/O is usually sufficient. For random access, memory mapping might be more suitable. For extremely large files that exceed available RAM, consider using specialized libraries like Dask
or Vaex
that handle out-of-core computation.
The above is the detailed content of How Do I Read and Write Files in Python?. For more information, please follow other related articles on the PHP Chinese website!