Lesson · 40 min · Free
Python File Handling Guide
Python File Handling Guide body { font-family: sans-serif; line-height: 1.6; margin: 20px; } h1 { color: #333; } h2 { color: #555; } pre { background-color: #eee; padding: 10px; border-radius: 5px; overflow-x: auto; } co
Python File Handling Guide
Welcome to this module on Python File Handling, a crucial skill for anyone working with data, especially in fields like pharmacy and biotechnology. In these disciplines, you frequently encounter data stored in various file formats, ranging from simple text files containing experimental results or patient records to complex CSV (Comma Separated Values) or JSON files from high-throughput screens or genomic analyses. Efficiently reading from and writing to these files programmatically is fundamental for data processing, analysis, and automation. Python provides robust and intuitive mechanisms for interacting with the file system. Understanding file handling allows you to ingest raw data, store processed results, generate reports, and exchange information with other software or collaborators. We'll cover the basics of opening files, reading their contents, writing new data, and ensuring proper file closure, which is vital for data integrity and resource management.
Opening and Closing Files
The primary function for file handling in Python is open() . This function takes at least two arguments: the file path (a string indicating the location of the file) and the mode (a string specifying how the file will be used). Common modes include: 'r' : Read mode (default). Opens a file for reading. If the file doesn't exist, an error occurs. 'w' : Write mode. Opens a file for writing. Creates a new file if it doesn't exist, or truncates (empties) the file if it does. Use with caution, as it overwrites existing content! 'a' : Append mode. Opens a file for appending. Creates a new file if it doesn't exist, or adds new content to the end of an existing file without truncating it. 'x' : Exclusive creation mode. Creates a new file and opens it for writing. If the file already exists, the operation fails. 'b' : Binary mode. Used in conjunction with other modes (e.g., 'rb' , 'wb' ) for handling non-text files like images or compiled data. 't' : Text mode (default). Used in conjunction with other modes (e.g., 'rt' , 'wt' ). After performing operations on a file, it's crucial to close it using the close() method. This releases the file resource and ensures that any buffered writes are actually committed to disk. Failing to close files can lead to data corruption or resource leaks. A safer and more Pythonic way to handle files is using the with statement, which ensures the file is automatically closed even if errors occur. # Example 1: Writing to a file and then reading from it # Using 'w' mode to write (and create if not exists, or overwrite if exists) file_path = "experimental_results.txt" with open(file_path, 'w') as file: file.write("Experiment ID: A101\n") file.write("Drug Concentration: 100 µM\n") file.write("Observed Effect: 85% inhibition\n") file.write("Date: 2023-10-26\n") print(f"Data written to {file_path}") # Using 'r' mode to read the content print(f"\nReading content from {file_path}:") with open(file_path, 'r') as file: content = file.read() print(content) # Using 'a' mode to append new data print(f"\nAppending new data to {file_path}:") with open(file_path, 'a') as file: file.write("Notes: Replicate 1 showed similar results.\n") print("New data appended.") # Reading the updated content print(f"\nReading updated content from {file_path}:") with open(file_path, 'r') as file: updated_content = file.read() print(updated_content) When reading files, you have several methods available: file.read() : Reads the entire content of the file as a single string. file.readline() : Reads one line from the file at a time. file.readlines() : Reads all lines from the file and returns them as a list of strings, where each string is a line. Iterating directly over the file object (e.g., for line in file: ) is generally the most memory-efficient way to read large files line by line. # Example 2: Reading a large file line by line for processing # Simulate a large data file (e.g., gene expression data) large_data_path = "gene_expression_data.csv" with open(large_data_path, 'w') as file: file.write("GeneID,Sample1,Sample2,Sample3\n") for i in range(1, 101): # 100 genes file.write(f"GENE_{i},{i*10},{i*10+5},{i*10-2}\n") print(f"Simulated large data written to {large_data_path}") # Process the file line by line to calculate average expression for each gene print(f"\nProcessing {large_data_path} line by line:") gene_averages = {} with open(large_data_path, 'r') as file: header = next(file) # Skip the header line print(f"Skipping header: {header.strip()}") for line in file: parts = line.strip().split(',') if len(parts) == 4: # Ensure it's a valid data line gene_id = parts[0] try: # Convert sample values to float and calculate average sample_values = [float(parts[1]), float(parts[2]), float(parts[3])] average_expression = sum(sample_values) / len(sample_values) gene_averages[gene_id] = average_expression except ValueError: print(f"Warning: Could not parse numerical data for gene {gene_id} in line: {line.strip()}") # Print a few processed results print("\nAverage expression for a few genes:") for gene, avg in list(gene_averages.items())[:5]: # Print first 5 print(f"{gene}: {avg:.2f}") This line-by-line processing is particularly useful in bioinformatics and pharmacology, where datasets can be gigabytes in size. Reading the entire file into memory at once (with .read() or .readlines() ) could exhaust available RAM, leading to program crashes. Iterating line by line allows you to process data chunks without loading the whole file.
Key Takeaways
Use open() with the appropriate mode ( 'r' , 'w' , 'a' , etc.) to interact with files. Always close files after use, ideally using the with statement for automatic resource management. 'w' mode overwrites existing files; use 'a' to append or 'x' for exclusive creation to avoid accidental data loss. For large files, iterate line by line ( for line in file: ) to conserve memory. File handling is essential for ingesting raw data and outputting processed results in scientific computing.
Practice Exercise
Imagine you have a text file named patient_data.txt that contains patient IDs and their corresponding blood pressure readings (systolic/diastolic) on separate lines. Each line looks like this: P101,120/80 . Your task is to: Create this patient_data.txt file programmatically with at least 5 lines of sample data. Read the file line by line. For each patient, parse the blood pressure readings. Calculate the average systolic and average diastolic pressure across all patients. Print the patient ID and their blood pressure, and finally print the overall average systolic and diastolic pressures. Consider how you would handle potential errors, such as a malformed line in the file.
Watch the full lesson — free
This topic is part of Python Programming - Basics, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →