Introduction to Python Programming/Python Programming - Handling Files

7. Files
Python provides for more input and output functions. The simplest method of producing output is using the “print” statement.

There are two methods in which the Python interpreter reads the input provided at the terminal namely:


 * 1) raw_input

This command reads whatever is at the terminal as the input regardless of whether it is an expression or not and returns a string.


 * 1) Input

The input command evaluates the expressions as regular Python expressions and takes it as input.

The following examples highlight the differences between raw_input and input.

>>> a=raw_input("enter your test here : ") enter your test here : new >>> a   'new' >>> print a   new >>> b=raw_input("enter your python expression :") enter your python expression : (1,2,3) >>> print a   new >>> print b   (1,2,3) >>> c=input("enter your text here :") enter your text here :"sunami" >>> print c   sunami >>> d=input("enter your text here :") enter your text here :(1,2,3) >>> print d   (1, 2, 3) >>> print type(d)  >>>e=input("enter your text here :") enter your text here :[x*x for x in range(5)] >>> print e   [0, 1, 4, 9, 16] >>> f=raw_input("enter your python expression :") enter your python expression :[x*x for x in range(5)] >>> print f   [x*x for x in range(5)] >>>

7.1.	Opening and Closing Files
Till now, we were writing at the input terminal and obtaining output at the terminal. Let’s move further to reading and writing files. Python provides for a plethora of functions to read and write files that may be text, or data files. File manipulation commands such done using a file object.

A list of couple of functions for operations on files are highlighted as examples within explanation provided with the comments. A file can be opened for reading, writing, appending existing text.

The “open” function opens a file that can used to read, write, or append data. For performing any of the above mentioned functions, every file must be opened first.

>>> myfile=open("example.txt", "w") >>> myfile.write("Oh my god!! I just opened a new file ! \n") >>> myfile.write("I typed in a whole lot of text \n") >>> myfile.write("Thank god, its ending ! \n") >>> myfile.close >>>

Please remember to close the file and then check if the actual file has the contents written into the file. Now, let’s go ahead and read the contents of the file that was just written.

>>> myfile=open("example.txt", "r") >>> for a in myfile.readlines: print a   Oh my god!! I just opened a new file ! I typed in a whole lot of text Thank god, its ending ! >>>   >>> myfile.close >>> myfile=open("example.txt", "w") >>> myfile.write("Start of a new chapter ! \n") >>> myfile.write("The second line in charter! \n") >>> myfile.close >>> myfile=open("example.txt", "a") >>> myfile.write("Oh my god!! I just opened a new file ! \n") >>> myfile.write("I typed in a whole lot of text \n") >>> myfile.write("Thank god, its ending ! \n") >>> myfile.close >>> myfile=open("example.txt", "r") >>> print myfile  >>> myfile.close

There is a Python module “os” that provides methods to perform file-processing operations such as renaming and deleting files. To get the current working directory, do the following.

>>> import os   >>> print os.getcwd C:\Users\Desktop\Projects\Upwork I already have an existing file named example.txt which I will rename using the Python os utility functions. >>>os.rename("example.txt", "sample.txt")