Python provides excellent capabilities for working with files. Whether you need to read data from a file, write data to a new file, append data to an existing file, or perform any other file operation, Python has great built-in modules like open(), CSV, json, os, and more to handle all of your needs.
This comprehensive guide will explore the various methods for reading and writing files in Python.
Before you become a techie, Read this guide: Think Twice Before Becoming a Techie.
Reading Text Files
We use the built-in open() function to read a text file in Python. This will return a file object that contains the contents of the file. Here is the basic syntax:
file = open('data.txt', 'r')
contents = file.read()
file.close()
The first parameter to open() is the filename and path. The second parameter is the mode for opening the file. 'r' means open the file for reading. Once we have the file object, we can call read() to get the contents as a string. It's also good practice to close the file when we're finished with it.
Instead of reading the entire contents at once, we can also read line by line using a for loop:
file = open('data.txt', 'r')
for line in file:
print(line)
file.close()
This iterates through each line in the file and prints it out.
Finding a job has become harder with this guide. This guide explores 10 hacks to get your desired job: 10 Hacks to Find a Remote Job in 2024
To read a CSV file, we need to import the CSV module. We can then use the csv.reader()
method to parse the CSV data:
import csv
with open('data.csv') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
print(row)
The statement handles opening and closing the file for us. The csv.reader()
creates a reader object that we can iterate to get each row as a list.
Writing Text Files
To write to a new text file in Python, we need to open the file in write 'w' mode.
file = open('output.txt', 'w')
file.write('This text will be written to the file')
file.close()
The 'w' mode will create the file if it doesn't already exist. Be careful, as it will overwrite the file's contents if it does exist.
We can also write line by line using a for loop:
lines = ['First line', 'Second line']
file = open('output.txt', 'w')
for line in lines:
file.write(line)
file.write('\n')
file.close()
This will write each element of the lines list to the file along with a newline character (\n).
To append to an existing file, we use 'a' append mode:
file = open('output.txt', 'a')
file.write('This will be appended')
file.close()
Writing CSV Files
We need to import the CSV module to write data to a CSV file. We can then create a csv.writer
object and use its writerow()
method to write each row:
import csv
fields = ['Name', 'Age']
rows = [
['John', 30],
['Mary', 28]
]
with open('output.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
# write the header row
writer.writerow(fields)
# write the data rows
for row in rows:
writer.writerow(row)
The csv.writer() will take care of quoting strings with commas, etc, so you can pass simple lists and dictionaries.
Using the With Statement
Whenever we open a file, we need to remember to close it to free up resources. The best way to handle this is using the statement:
with open('data.txt') as file:
contents = file.read()
# file closed automatically here
This closes the file for us as soon as we exit the block.
Working with File Paths
When specifying file paths, we usually want them to work across systems. We can use the os.path
module to deal with this:
import os.path
print(os.path.join('files', 'data.txt'))
# Prints 'files/data.txt'
os.path.join()
will create the right path separator for whatever system it runs on.
To get just the file name or directory from a path:
print(os.path.basename('/path/to/data.txt')) # data.txt
print(os.path.dirname('/path/to/data.txt')) # /path/to
Checking for Files
We can check if a file or directory exists using os.path
:
import os.path
print(os.path.exists('data.txt')) # True if exists, False otherwise
print(os.path.isfile('data.txt')) # True if file, False otherwise
print(os.path.isdir('folder')) # True if folder, False otherwise
This becomes useful when writing scripts that must be checked for files before accessing them.
Full Example
Here is an example script that combines some of these concepts to search for files, read their contents, and write to a new file:
import os
import csv
results = []
# Search all csv files in cwd
for csv_file in os.listdir('.'):
if not csv_file.endswith('.csv'):
continue
print(f'Processing {csv_file}')
with open(csv_file) as file:
csv_reader = csv.reader(file)
for line in csv_reader:
results.append(line)
# Write summary
with open('summary.txt', 'w') as file:
for row in results:
file.write(f'{row[0]}, {row[1]}\n')
print('Done!')
This shows how we can combine these file operations to create useful Python scripts for processing data from files.
Conclusion
Python provides a variety of powerful tools for handling file input and output. The open() function and with statement make it easy to work with file contents.
For CSV data, the CSV module parses it into lists and dictionaries. The os.path module helps deal with platform-independent file paths.
With this foundation, you can write Python scripts to automate loading, processing, and saving data from various file formats. File handling is an essential skill for any Python programmer.
This covers the key concepts for reading and writing files in Python, including text, CSV, and other formats. For more details, check out the official documentation for these modules. The csv module and statement context manager provide convenient ways to handle most file operations.
If you like my work and want to help me continue dropping content like this, buy me a cup of coffee.
If you find this post exciting, find more exciting posts on Learnhub Blog; we write everything tech from Cloud computing to Frontend Dev, Cybersecurity, AI, and Blockchain.
Top comments (0)