Reading and Writing External Files From Python Code
Python programming language has various functions and statements for working with a file. The with statement and open() function are two of those statements and functions. Here we will discuss how we can use both statements to work with files in Python.
What is open() Statement in Python
In Python we can work with files like in other programming languages. You have to open the file first. So, the open() function opens a file so that can work with the file. To use the open function, you declare a variable for it first. The open() function takes up to 3 parameters – the filename, the mode, and the encoding. You can then specify what you want to do with the file using other functions. Consider the following code to open file.
my_file = open("hello.txt", "r")
print(my_file.read())
my_file.close()
# Output :
# Python will print the contents of the file.
# Note : File must be there before you open it
Note: The open() function does not close the file, so you also have to close the file with the close() method. The read mode is the default file mode in Python, so if you don’t specify the mode, the code above still works fine.
Note : It is also assumed that the file is in the same folder as the Pythons program thats opening it. If the file is not in the same folder then you have to specify the path to that file. Consider the example
# Open function to open the file "MyFile1.txt"
# (same directory) in append mode and
file1 = open("MyFile.txt","a")
# store its reference in the variable file1
# and "MyFile2.txt" in D:\Text in file2
file2 = open(r"D:\Text\MyFile2.txt","w+")
With
Statement in Python
The with statement works with the open() function to open a file. So, you can re-write the code we used in the open() function example like this:
with open("hello.txt") as my_file:
print(my_file.read())
# Output :
# Hello world
# I hope you're doing well today
# This is a text file
Unlike open() where you have to close the file with the close() method, the with statement closes the file for you without you telling it to. This is because the with statement calls 2 built-in methods behind the scene – enter() and exit(). The exit() method closes the file when the operation you specify is done.
with open("hello.txt", "w") as my_file:
my_file.write("Hello world \n")
my_file.write("I hope you're doing well today \n")
my_file.write("This is a text file \n")
my_file.write("Have a nice time \n")
with open("hello.txt") as my_file:
print(my_file.read())
# Output:
# Hello world
# I hope you're doing well today
# This is a text file
# Have a nice time
Reading from a file
There are three ways to read data from a text file.
read() : Returns the read bytes in form of a string. Reads n bytes, if no n specified, reads the entire file.
File_object.read([n])
readline() : Reads a line of the file and returns in form of a string.For specified n, reads at most n bytes. However, does not reads more than one line, even if n exceeds the length of the line.
File_object.readline([n])
readlines() : Reads all the lines and return them as each line a string element in a list.
File_object.readlines()
Note: ‘\n’ is treated as a special character of two bytes
Writing in a File
There are two ways to write in a file.
write() : Inserts the string str1 in a single line in the text file.
File_object.write(str1)
writelines() : For a list of string elements, each string is inserted in the text file.Used to insert multiple strings at a single time.
File_object.writelines(L) for L = [str1, str2, str3]
We can also loop through the file and print the text line by line. Consider the code as under:
with open("hello.txt", "w") as my_file:
my_file.write("Hello world \n")
my_file.write("I hope you're doing well today \n")
my_file.write("This is a text file \n")
my_file.write("Have a nice time \n")
with open("hello.txt") as my_file:
for line in my_file:
print(line)
# Output:
# Hello world
# I hope you're doing well today
# This is a text file
# Have a nice time
Now question here is which way you should use to work with files between the combo of with and open() and just the open() function. I would suggest you to use the with and open() combination because the with statement closes the file for you and you get to write less code.
Type of File Handling
Python provides inbuilt functions for creating, writing, and reading files. There are two types of files that can be handled in python, normal text files and binary files (written in binary language, 0s, and 1s).
Text files: In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new line character (‘\n’) in python by default.
Binary files: In this type of file, there is no terminator for a line, and the data is stored after converting it into machine-understandable binary language.
File Access Modes
File access modes inform the type of operations possible in the opened file. It refers to how the file will be used once its opened. These modes also define the location of the File Handle in the file. File handle is like a cursor, which defines from where the data has to be read or written in the file. There are 6 access modes in python.
Read Only (‘r’) : Open text file for reading. The handle is positioned at the beginning of the file. If the file does not exists, raises the I/O error. This is also the default mode in which a file is opened.
Read and Write (‘r+’): Open the file for reading and writing. The handle is positioned at the beginning of the file. Raises I/O error if the file does not exist.
Write Only (‘w’) : Open the file for writing. For the existing files, the data is truncated and over-written. The handle is positioned at the beginning of the file. Creates the file if the file does not exist.
Write and Read (‘w+’) : Open the file for reading and writing. For an existing file, data is truncated and over-written. The handle is positioned at the beginning of the file.
Append Only (‘a’): Open the file for writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.
Append and Read (‘a+’) : Open the file for reading and writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.