This tutorial guides you about some basic concept of File handling in Python.
We can open the file in a number of ways according to need For example – Writing, Reading, Adding more content to file, etc. Its file handling functions are little similar to C language.
1. Different Modes
Before start working with files we first need to open it. While opening the file we need to decide in what mode we want to open the file. For this purpose there are different modes are available.
For example, for creating a new file or overwriting contents in existing file there is w mode and for reading a file there is r mode. There are some other modes for the different-different purpose.
Some modes with description are –
|w||It searches the file.If the file exists it contains are overwrite, if the file does not exist a new file is created.|
|r||It searches the file if the file exists then stream is positioned at the beginning of the file|
|a||It searches the file if the file exists then stream is positioned at the end of the file. If the file does not exist a new file is created.|
|w+||It searches the file if the file exists it contents are overwritten and if the file does not exist a new file is created. It opens the file for both reading or writing.|
|r+||It searches the file if the file exists then stream is positioned at the beginning of the file and if it doesn’t exist it return null. It opens the file for both reading and writing.|
|a+||It searches the file if the file exists then stream is positioned at the end of the file and if it doesn’t exist it create a new file for reading and writing.|
2. Opening file with different-different modes
For opening a file in Python for these we use the open() function. This function returns a file object.
variable = open(filename, mode)
The first parameter is the name of the file and the second parameter is the mode ( w, r, a, etc.) , by these we specify what we are performing with the file. For example – if we have opened a file in w mode then we can’t able to read the file.
Creating a file
fw = open("myfile.txt","w") # open file in write mode fw.write("My new File\n") fw.write("Hello") fw.close()
Here, we have created a new text file myfile.txt and write some text on it using write method. When you run it you will see a new file myfile.txt is created in your current project directory.
write() method is used to write content to a file, it takes only one parameter which is the string variable or string. It returns the number of characters written. It does not add a new line character at the end of string for this you can use \n
file-object.write( string )
Reading a file
When we open the file in read mode then stream is positioned at the beginning of the file.For reading data from the file we have two functions read() and readline().
fr = open("myfile.txt","r") text = fr.read() print(text) fr.close()
Here, we have open a file (myfile.txt) in read mode and whenever we read data we need to store it for using it, for this we created a variable text and for reading data from file we use read() method.
read() method is used to read data from a file. It takes an optional parameter number of bytes to be read and if it is not defined then it tries to read the whole file.
variable = file-object.read([count])
readline() method allow us to read an open file line by line. if
readline() returns an empty string it means the end of the file has been reached.
variable = file-object.readline()
fr = open("myfile.txt","r") text = fr.readline() print(text) fr.close()
If your file contains a large number of lines it’s better to use a loop. In the loop, we have to loop over file object.
fr = open("myfile.txt","r") for line in fr: print(line) fr.close()
Append in file
When we open the file in append mode then stream is positioned at the end of the file.
fr = open("myfile.txt","a") fr.write("New text") fr.close()
Here, we have open a file in append mode and writing content on it using write() method. It writes new text at the end of the file.
3. Closing File
Whenever we were done working with a file is good to close its file object. It frees up some extra memory which is occupied by file object.
For closing a file we use a close() method. It doesn’t take any parameter
fw = open("myfile.txt","w") fw.write("My new file") fw.close()