In this article we will look at how python handles files and file operations. File handling is an important part of applications and software. And to create functional apps, we need to learn how we can read, write or modify files according to our use. To read a file in python we use, ‘r’ , to write in a file we use ‘w‘ and much more.
The w in open(filename, “w”) means that the file being opened will be in write mode and you can make changes to it.
In this tutorial, we look at how to write in files with python.
How to open a file in python?
The syntax is one of the main reasons why python is so popular. Keeping up with its popularity, opening a file in python is very easy. You just have to write “open” which is a simple function that takes in two parameters as input. The first one is the file name and the second one is the mode in which we want to open the file in. There are different modes in which we can open() a file.
#file=(filename, mode) fileopened= open("FILENAME.txt",'w')
in the above code we open a text file in write mode and assign it to a variable .
Closing a file in python
We can close a file after finishing our work using the file.close() function.
Text and binary files
Let’s look at all those modes one by one. But before that we need to find out what all type of files can be manipulated using the open() function.
The open() function can open files in text(“.txt”) mode or in binary (“b”) mode. The “.txt” extension is used to read text files and the “.b” extension is used to read binary files containing more than just texts such as images.
To know more about text and binary files, click here.
Types of file utilization
There are a few modes in which a file can be opened, they are as follows:
- r: To read a file, we need to open it in read only mode by using ‘r’.
- w: To write in an existing file. It will overwrite anything that already exists in a file. If there are no existing files, then it creates a new file with the given name.
- r+: To read and write data in a file but the existing data will be erased and new data will be written.
- w+: This is used to write and read the data in a file but all existing data gets erased and new data is written and stored.
- a: The append mode is used to preserve existing data in a file and add more data in it.
But in this tutorial we will only look at how we can write in a file in only ‘w’ mode.
Writing in a file using “w” mode
Let us write in a file using the write() function by opening it in w mode.
Let’s first create a text file in our working directory. I have named it “test.txt” you can name it whatever you want. The file will be initially empty but we will write the following in the file.
#opening the file file= open("test.txt",'w') #writing in the file using write() file.write("This is a test file.") file.write("Hello World.") #closing the file file.close()
Now if you open the file you created you will see that there is text in the file.
This is a test file.Hello World.
Now we will use the writelines() function to write multiple lines in one go. Let us look at how that can be done.
#opening the file file= open("test.txt",'w') #creating a list of input L= #size of the list N=int(input("enter number of lines=")) #taking user input for i in range(N): element=input("enter sentence to write into your file=") L.append(element + '\n') #writing the lines in the file file.writelines(L) #closing the file file.close()
Now if you reopen the file, you can see that the inputs that you gave have been successfully written inside the file on separate lines.
This is a test file We are using writelines function It is an efficient way to write in files
File manipulation is an important aspect of every programming language and python is no different. We can easily open and close files in python using specific functions. Python also allows us to write, read and append data in a file in a flexible manner. This article covers the basics of file handling followed by how to write in a file in “w” access mode. To know more, check out this fabulous article on file handling on askpython!