Python allows file handling and supports reading, and writing along with some other operations in a file. In Python, every file is treated as a text or binary file. Each line of code is a sequence of characters. Each line is terminated with special characters, End of File(EOF), like commas and newline characters.
In Python, different file operations take place, and we will discuss about them one by one –
- Open a file
- Close a file
- Read or write in the file
Opening Files in Python –
In Python, we have an in-built open() function to open a file. And this function returns a file object to perform other operations accordingly.
f = open("new.txt") #we can either write the simple name of the file f = open("C:/Python38/text.txt") #we can also write the full path of the file
We can also specify the mode while opening the file, whether we want to open it in reading, writing, or append mode.
We can open a file in two modes –
- Text mode
- Binary mode
The default mode is the read mode in text format. Here we get strings while reading. And the binary format returns bytes and is used to deal with non-text files or executable files.
Different modes for opening a file –
|r (read mode)||Open the file for reading. It is the default mode.|
|w (write mode)||Open the file for writing, if it exists. And if there exists no such file, then it will create one, and then open it in the write mode.|
|a (append mode)||Open the file to add data at the end of the file, if it exists. And if the file doesn’t exists, it creates one and then opens it in the append mode.|
|t (text mode)||Open the file in text mode.|
|b (binary mode)||Open the file in binary mode|
|+||Open the file for updating something (basically read and write operations).|
|x (exclusive mode)||Open a file for exclusive creation. and if the file already exists, the operation fails.|
Closing Files in Python –
When we have performed all the operations on the file, then we can close it. There is an in-built function for this as well. It is done by using the close() method
f = open("new.txt" , 'w') #opening the file #lets say , we have performed certain operations on the file , and now we want to close it f.close() #and the file f will be closed.
However, we don’t need to call the close method explicitly, every time. It can also be done internally.
Read and Write operations in Python –
For the read operation, we can just open the file and read it using the open() function.
But for the write operation, we need to open it in write, append, or exclusive mode. We must also use this write (w) operation carefully because it can also overwrite the already existing data in the file.
The write() function returns the number of characters written in the file.
with open("new.txt" , 'w') as file:#opening the file f.write("writing the first line\n") f.write("writing the second line\n") f.write("writing the third line\n") #performing the write operation in the file in the write mode
This program will overwrite the existing file in the directory. And also will create a new file and write in that, if it doesn’t exists.
Python File Methods –
There are various methods available with the file object.
Some of them are listed below –
|close()||Closes an opened file.|
|read(n)||Reads at most n characters from a file. If the provided n, is negative, then it reads till the end of the file.|
|write(s)||Writes the string s to the file and returns the number of characters written.|
|truncate(size = None)||Resizes the file to the size of bytes. If not specified, then it resizes it to the current location.|
|writable()||It returns true if a file stream can be written.|
|writelines(lines)||It writes the lines in the given file.|
|tell()||Returns an integer that tells the current location of the file object.|
|seek(offest , |
|Changes the file position in offset bytes, in reference to from (start, current, end)|
|fileno()||Returns an integer file number(file descriptor).|
|flush()||Flushes the write buffer of the stream.|
Special thanks to Ishita Dhiman for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article. If you want to suggest any improvement/correction in this article please mail us at [email protected]