Python can read and write files, which is very useful for storing data persistently.
The key function for working with files in Python is the open() function.
-
When opening a file, we can specify the mode we want to open it in (
read,write,append, etc.).-
The
open()function takes two parameters;filename, andmode.f = open("demofile.txt", "r")
-
-
There are four different methods (modes) for opening a file:
"r"- Read - Default value. Opens a file for reading, error if the file does not exist"a"- Append - Opens a file for appending, creates the file if it does not exist"w"- Write - Opens a file for writing, creates the file if it does not exist"x"- Create - Creates the specified file, returns an error if the file exists
-
In addition you can specify if the file should be handled as binary or text mode
"t"- Text - Default value. Text mode"b"- Binary - Binary mode (e.g. images)"+"- Read and Write - will allow you to read and write to files
-
It's important to close the file when you are finished with it.
f.close()
- if you don't close the file, Python will not write to the file properly.
The with statement simplifies exception handling by encapsulating common preparation and cleanup tasks.
-
In addition, it will automatically close the file. The
withstatement provides a way for ensuring that a clean-up is always used.with open("text.txt", "a") as f: f.write("Now the file has more content!", end="\n") # instead of: f = open("text.txt", "a") f.write("Now the file has more content!", end="\n") f.close()
Hello World! Now the file has more content!
-
The
read()method returns the specified number of bytes from the file.-
Default is
-1, which means the whole file.Hello World!
with open("text.txt", "r") as f: print(f.read()) # read the whole file
-
-
readline()method returns one line from the file.Hello World! Hello Python!
with open("text.txt", "r") as f: print(f.readline()) # read the first line -> Hello World!
-
readlines()method returns a list of lines from the file.Hello World! Hello Python!
with open("text.txt", "r") as f: print(f.readlines()) # read all lines -> ['Hello World!\n', 'Hello Python!']
To write to an existing file, you must add a parameter to the open() function: "a" - Append - will append to the end of the file.
-
if the file does not exist, it creates a new file for writing or overwrites an existing file.
Hello World!
f = open("text.txt", "a") f.write("Now the file has more content!", end="\n") f.close()
Hello World! Now the file has more content!
CSV stands for Comma Separated Values.
-
CSV files are used to store a large number of variables – or data.
-
We can read CSV files just like other text files. using
readLines()method, and separate the values using commas.with open("data.csv", "r") as f: data = f.readlines() for line in data: print(line.split(","))
-
But there is a module named
csvwhich makes CSV parsing easier.Name,Age,City John,25,New York Sarah,28,Paris
import csv with open("data.csv", "r") as f: data = csv.reader(f) for line in data: print(line) # ['Name', 'Age', 'City'] # ['John', '25', 'New York']
import csv with open("data.csv", "r") as f: data = csv.reader(f) for line in data: print(line[0], line[1], line[2]) # Name Age City # ...
-
We can also read CSV files into dictionaries.
-
This is more defensive way of reading CSV files. because we can access the values by their names, even if the order of the values in the CSV file changes.
Name,Age,City John,25,New York Sarah,28,Paris
import csv with open("data.csv", "r") as f: data = csv.DictReader(f) for line in data: print(line) # {'Name': 'John', 'Age': '25', 'City': 'New York'} # {'Name': 'Sarah', 'Age': '28', 'City': 'Paris'}
import csv with open("data.csv", "r") as f: data = csv.DictReader(f) for line in data: print(line["Name"], line["Age"], line["City"]) # Name Age City # ...