Python like any other language allows file handling and I/O functions. There are many ways with which we can approach file handling and input output processing. In this tutorial we’ll see how to work with I/O functions of python. We’ll also take a look at the ways with which python allows us to handle files.
Note: There are different modes of reading and writing the file, these modes can be found in official python documentation. I have skipped those modes in this tutorial and used only necessary modes for examples.
Python Input Functions
In order to take the input from keyboard you have to use two built-in functions.
input raw_input (No longer supported in python 3.x)
The input function as per new python 3.x works exactly like raw_input and it also accepts the valid python expression for input via keyboard. So you can still send raw input characters via keyboard to input function in new python 3.x. Check the following code in 2.7 and 3.x version of python.
str = raw_input("Enter some random text: ") print "you input ", str str1 =input ("Enter another random text ") print ("random text mylord is" + str1) # 3.0 change -usage of print()
You can read more about the change in raw_input and input here.
Note that version 3.x is going to force you to write print() instead of print with soft-space. Get ready for this change because sooner or later you’re going to migrate from python 2.x to 3.x eventually.
You can still use print statement in python 2.x with soft-space. Check the following code.
str="hello" print str; #works with python 2.x str="hello" print(str); # necessary to use print() from 3.x
Python File Management
In our previous examples we checked how to manipulate keyboard input and output. Now we have to extend this to binary files. We’re going to learn how to open file, read or write on it and finally will look at how to close the files.
Basically we’re doing the following steps in almost every file read or write operation.
- Get a file object and open the file using open() function
- Read or write to the file.
- Close the file.
- Print or process the contents of file.
The Open() and Close() Functions
In order to open a file you have to use file object to open the file with the help of open() function. Check the following code :
f1 = open("example.bin","rb"); fc=f1.read() f1.close() print("Opened File"); print(fc);
This program will open demo.txt file on your hard drive and will store the contents in f1 object. We’re using read() function to readback entire content of the f1. After reading the file we’re closing the file access. It is a good practice to close the file using close() method after your usage. At the end we’re printing the content of file by accessing the file object fc.
The open() function takes file name and access mode as input. You have to enter the name of file and also “r or w” as an access mode. You can use rb or wb if you’re using windows operating system as it differentiates between binary and ascii files. There are many ways of access modes which you can read in python documentation, for the sake of simplicity we’re looking at simple “r” and “w” modes in this tutorial.
Reading and Writing Files
We’ve already used the read() method in our previous program. In this program we’ll take a look at write() function. The write() function writes to any opened file by file object. You need to make sure that you’re using the right access mode. If in doubt always use “w+” or “r+” mode just in case if you want to perform multiple manipulation on file.
Note that write() method can’t add “\n” to the end of the string.
fstr = open("randomtest.txt", "wt") fstr.write("In Random Universe\nWe're having fun!") fstr.close()
In order to read the contents of the file :
fstr1 = open("randomtest.txt", "rt") t1 = fstr1.read() fstr1.close() print(t1)
That’s it. Your file output will be shown. In the first part we have opened the file “randomtest.txt” and wrote the new content in it. After the writing operation we closed the file. In second part we’re opening the file and reading it with the help of another object. At last we’re closing the file and then printing our new object that holds the data of our file.
You can also use the snippets in this tutorial along with python regex tutorial to make some cool regular expression programs. Hope this tutorial helps you to understand file handling and Input in python. Feel free to submit your feedback in comments.