Writing data to the file is another important topic need to be discussed for Data file handling in Python. We can write data to file by using write() or writelines() function. write() function takes string as argument where writelines() takes list of lines as argument. But before all these thing we need to open the file object by using open() function with w or a as file mode. For example –
fout = open(‘address.txt’, ‘w’) here file mode used is w in place of that we can use a as file mode. The difference between w and a is when you will open a file with write mode it will create a new file called address.txt or else if file exist will delete all existing data from the file and start writing from the first line. Where as when you open a file using append mode will create a new file if not exist else will start writing to the file at the last position in a file. Now lets see the example of writing data into file –
The output of the program will look like this –
The first output section is taking input from user till your says n. And next output is displaying all the content appended at the list of the file.
If want to discuss something, feel free to comment. Thank you.
Python supports file handling and allow to read and write data from file . Python supports two kind of files 1. text file 2. binary file. Text file is used to read and write data into file where data will store in human readable format and binary file will store the data in binary format which is readable using program only.
Now without wasting our time lets jump into example. So here is the example –
In this example we opened addr.txt file in read mode. The default mode is read mode only so you can write open(‘addr.txt’) is also valid. Now once the connection is established between file and file object you can call read() function on the file object. Next step is to print. Last but very important work is to close the connection using close() function. Here is the output-
That’s it. In next chapter we will discuss about reading data using readline() and readlines().
Serialization and de-serialization is the process to store python object structure into file and viceversa. All because of using pickle module this process is known as pickling (serialization) and un-pickling(de-serialization). Pickling can get achieved by dump() method and un-pickling can be achieved by load() method of pickle module. But using this module in your program first you need to import pickle in your file. Here is the example of pickling(serialization).
This code will create a class called Student and at line no 29 Object of Student is created stu1. After that data is being loaded using readMarks() method at line no 31. One data is loaded, created file object f1 of ‘student.log’ file at line no 34. Later on used pickle.dump() method to dump the data into file and at last closed() the file object.
Handling Text file in Python (Writing Data in Text file)
Storing data in text file is one of the important work people use to do in any language. If you will go for C++ and Java, you have to write lots of code for doing and making connection of stream explicitly and have to import many additional libraries to do so.
But here in case of python you no need to worry about all these. You have to write just few line of code to write data into text file. Here is the sample code.
fout = open("StuTextFile.txt","a+")
name = input("Enter your name : ")
addr = input("Enter address : ")
school = input("Enter school : ")
str = name+"\t"+addr+"\t"+school
Code description –
In first line of code a file “StuTextFile.txt” will get open in append mode so that data entered will get saved again and again without overwriting it in to file. As you know default file mode is r+ to read, w+ for write and a+ to append.
Second, third and fourth line is just to get input from user (name, addr, school). Fifth line is creating a string with tab space. Sixth line is the important line to call write() function on the file object created (fout). Last line is to close the file object. It means file object’s connection with file will be disconnected.