Python File Opening

 

In python, the built-in open() function is the crucial function to work with the files. To perform any file-related operations such as creating the file, writing the file, reading the file, etc., you need to use an open() function.

 

The built-in open() function is useful to open the physical file and map to the file object to perform the file-related operations in python.

 

Following is the syntax of defining the open() function in python to open the files.

 

open(filename, mode)

The open() function will accept two parameters; those are

 

  • filename - It's the file's name, including the file path.
  • mode - The mode parameter is useful to indicate the purpose of opening the file, e.g., read, write, etc., and it's optional.

The following table lists the different modes available in python to perform create, write, update, and delete operations on files in python.

 

ModeDescription
x It is used to create the file.
w It is used to open a file for writing only.
w+ It is used to open a file for both writing and reading.
wb It used to open a file for writing only in binary format.
wb+ It is used to open a file for both writing and reading in binary format.
r It is used to open a file for reading only.
r+ It is used to open a file for both reading and writing.
rb It is used to open a file for reading only in binary format.
rb+ It is used to open a file for both reading and to write in binary format.
a It is used to open a file for appending only.
a+ It is used to open a file for both appending and reading.
ab It is used to open a file for appending only in binary format.
ab+ It is used to open a file for both appending and reading in binary format.

Python Open File to Write the Data

Following is the example of opening the file to write the data in python.

 

file = open("testfile.txt", "w")
file.write("Welcome to Tutlane")
file.close()

To learn more about writing the files in python, refer to Python writing data to the files.

Python Open File to Read the Data

Same way, you can use the open() function to open and read the data from a file.

 

file = open("testfile.txt")
print(file.read())

#or

file = open("testfile.txt", "r")
print(file.read())

For file reading operation, it's enough to mention the file name. By default, the open() will function consider using mode as "r".

 

The above examples wrote by considering the file (testfile.txt) that exists in the same python location. If the file is in a different location, you need to specify the complete file path to write or read the file as shown below.

 

file = open("D:\\python\testfile.txt", "r")
print(file.read())

In python, you can read the data from a file in different ways. You can learn more about it in Python read data from the file.

Python File Methods

Like, write(), read(), etc., the file object has different methods to perform the file-related operations in python.

 

MethodDescription
write() It is useful to write a specified string to the file.
writelines() It is useful to write a list of strings to the file.
writable() It will return whether the file is writable or not.
read() It is useful to read the file content.
readline() It will return the single line of text from the file.
readlines() It will return all the lines of file content as a list.
readable() It will return whether the file is readable or not.
close() It will close the file.
seek() It is useful to set the file's current position.
seekable() It will tell whether the file allows changing the position or not.
tell() It will return the file's current position.
flush() It is useful to flush the internal buffer.
truncate() It is useful to resize the file to a specified size.

In the next chapters, we will learn more about creating, writing, reading, and deleting files in python.