Reading and writing is one of the most fundamental things you will do in Python. There are many variations of this of whether you are working with a text file, a comma separated file, or even image files. We will explore all of these and more in this ultimate guide to reading and writing files!
For a given file, regardless of the variation of what type of file we are working with, the fundamental is the same: we need to open it first, we perform the read/write operation, then we close the file. Hence, we typically have these three steps for any file interaction. Sometimes though, the close maybe automatic when the program finishes, but it is best practice to manually close the file each time.
1. Read and Write a Text File in Python
When accessing a file, we could do many things such as read from it, write from it etc. In Python this is specified by providing a File Access Modes. This is the list of the most commonly used access modes which you can use when you work with text files:
Read Only | r |
Read and Write | r+ |
Write Only | w |
Append Only | a |
Append and Write | a+ |
a) Writing To A Text File In Python
Here we learn how to write data in the file
- Open a file using the open() in
w
mode. If you have to read and write data using a file, then open it in anr+
mode. - Write the data to the file using
write()
orwritelines()
method. - Close the file.
Let us look at the given example to understand it better:
#for opening a file in 'w'
file= open('sample.txt','w')
# write() - for write direct text to the file
# writelines() - for write multiple lines or strings at a time
file.write("How to program python.\nI am a programmer.")
file.close # closing the file
Now you will find sample.txt
in your directory.
b) Reading From A Text File in Python
In the above section, we see how to write data to a file, Now we learn how to read data that we have written into a file.
- Open a file using the
open()
inr
mode. If you have to read and write data using a file, then open it in ar+
mode. - Read data from the file using
read()
orreadline()
orreadlines()
methods. Store the data in a variable. - Display the data.
- Close the file.
Again let us look at the given example to understand it better:
Now our output display like this way;
# for opening a file in 'r'
file = open('sample.txt','r')
# read()- this reads all content from a file
# readline()- this reads one line by default or number of lines you specify
# readlines()-it used to read all the lines from a file, it returns a list
data = file.read()
print(data) # for printing the data
file.close() # for closing the file
Output-
How to program python.
I am a programmer
2. Read & Write A JSON File
A JSON (or JavaScript Object Notation) format is a popular file format which can be extremely helpful to manage complex objects in Python and convert them to a human readable, hierarchical, text format. JSON format does represent data types in different ways which can be seen here:
Python object | JSON object |
dict | object |
list, tuple | array |
str | string |
int, long, float | numbers |
True | true |
False | false |
None | null |
a) Writing an object to a JSON file in Python
We use json.dumps()
method in python for writing JSON to a file in Python.
It takes 2 parameters:
- dictionary – the name of the dictionary which should be converted to a JSON object.
- file pointer – pointer of the file opened in write or append mode.
#python program to write JSON to a file
import json
#Data to be written
dictionary = {"name":"charleswhite",
"zipcode" : 1230,
"address" : "polland"
}
with open("sample.json","w") as outfile:
json.dump( dictionary, outfile)
Output :
{ "name":"charleswhite","zipcode":1230,"address":"polland"}
b) Reading JSON from a file using python
The JSON package has json.load()
function that loads the JSON content from a JSON file into a dictionary.
It takes one parameter:
- File pointer: A file pointer that points to a JSON file.
# python program to read JSON from a file
import json
# opening JSON file
with open('sample.json','r') as openfile:
json_object=json.load(openfile) # Reading from json file
print(json_object)
print(type(json_object))
Output :
3. Reading And Writing Tab-Delimited File in Python
Tab-delimited files have traditionally been used to communicate between enterprise systems where both the structured nature of the data, and the small file size has been quite appealing in these scenarios. Typically each column is separated (or delimited) by a tab \t
character, and each row is separated by a newline character \n
.
myFile = open("somefile","r")
for aRow in myFile:
print( aRow.split('\t') )
myFile.close()
Writing Tab-Delimited File: The writing case is just opposite to the reading scenario. we use a “\t”.join( someList ) to create the tab-delimited row. Just see the example :
list_header = ['id', 'customer_name', 'address']
list_items = [ [ 1, 'Homer Simpson', '1 Rodeo drive'],
[ 2, 'Ned Flanders', '3 Rodeo drive'] ]
test = open("boats.tab","w")
test.write("\t".join( list_header ) )
test.write("\n")
for item in list_items:
test.write( "\t".join( item ) )
test.write("\n")
test.close()
4. Read and Write a Binary file in Python
For writing a binary file we use file.write()
In this scenario you must call open(file,mode)
with mode as “wb”
to open the file in write binary mode. Call file.write(str) with the opened file as the file to append a sequence of bytes str
.
file = open("document.bin","wb")
file.write("This binary string will be written to document.bin")
file.close()
For reading the file we can take our document.bin
file and used the “rb
” mode. Here we use our read() function to read the document.bin
file. The read() method returns the specified number of bytes from the file.
Here we use print(file.read(4))
so it will read only four bytes.
file= open("document.bin","rb")
print(file.read(4))
file.close()
5. How to Read and Write an Image in Python
To read an image we can use the library cv2.imread()
function. We provide a certain path for the image and that will read the file.
import numpy as np
import cv2
# Load an color image in grayscale
img = cv2.imread('Top-bike-wallpaper.jpg',0)
To display an image in a window, use cv2.imshow() function.
# Display the image
cv2.imshow('image',img)
# key binding function
cv2.waitkey(0)
# Destroyed all window we created earlier.
cv2.destroyAllwindows()
On running the following code, our image will look like this
For Writing an Image, we use a cv2.imwrite() function to save an image. Here we take the first argument as a file name and the second to save the image which I want.
cv2.imwrite('messigray.png',img)
6. Load a Dictionary or List to A File with Pickle in Python
Pickle is a library that preserves a given object’s structure when you save it to a file. You can think of it as a persistent storage of your data. To load a dictionary from a file with Pickle in Python, simply we use pickle.load(file) with the file object as a file to get the dictionary stored in it.
import pickle
file_to_read = open("data.pickle","rb")
loaded_dictionary = pickle.load( file_to_read )
print( loaded_dictionary )
Output :
{'a': 1}
7. Reading and Writing Lists to A File in Python
This is similar to reading / writing text files. Here, the intention is to write all the items which are separated by each line. When the file is ready, you can then reverse the operation and read in the file.
# List
item_list =['Charles','White','Python']
with open('cwp.txt','wt') as f:
for items in item_list:
f.write('%s\n' % items) # write elements of list
print("File written successfully")
f.close() # close the file
In the above example we see how to write a file. Now we see how to read a file by using r mode. The data read from the file is printed.
# open file in read mode
f= open('cp.txt','r')
# display content of the file
print( f.read() )
#close the file
f.close()
Output :
Charles
White
Python
8. Reading And Writing CSV Files in Python
A CSV (comma-separated values) file is a text file that has a specific format that allows data to be saved in a table structured format. To represent a CSV file, the convention is to name the file with the .csv file extension.
First we open CSV file using open() in r mode then use reader() method. Here is some syntax that will consider:
csv.reader(csvfile, dialect='excel', **fmtparams)
Let’s see it with an example :
import csv
with open('Giants.csv', mode='r')as file: # opening the CSV file
csvFile = csv.reader(file) # reading the CSV file
# display the contents of the CSV file
for lines in csvFile:
print(lines)
Output :
'Charles', 4, 'A' 'Robert', 8, 'B' 'Gravie', 5, 'A' 'Maggie', 4, 'C'
Here we use writer.csv to insert the data and some syntax which will consider as :
csv.writer(csvfile, dialect='excel', **fmtparams)
To write a single row, we use writerow() method was writing multiple rows we use writerows()
function.
Let’s understand with an example :
# Python program to demonstrate writing to CSV
import csv
fields=['Name','Class','Grade','Year'] # field names
# data rows of csv file
rows = [['Charles','4','A','2015'],
['Robert','8','B','2020'],
['Gravie','5','A','2022'],
['Maggie','4','C','2021']],
filename="university_records.csv" # name of csv file
# writing to csv file
with open(filename,'w') as csvfile:
csvwriter = csv.writer(csvfile) # creating a csv writer object
csvwriter.writerow(fields) # writing the fields
csvwriter.writerows(rows) # writing the data rows
Conclusion
Writing and reading of files is not a complex task and there are many ways to achieve the same thing either by using libraries, or by performing some of the transformation yourself at the point of file read/write.
Error SendFox Connection: