The final built-in object type of python allows us to access the files. The open() function creates a Python File object, which links to an external file. After a file is opened, you can read and write to it like normal. Files in Python are different from the previous types I’ve covered. They aren’t numbers, sequences, nor mappings; they only export methods for common file processing. Technically, files are a pre-built C extension that provides a wrapper for the C stdio.(standard input/output) file system. If you already know how to use C files, you pretty much know how to use Python Files.

Here’s the list of Python file Operations:
⦁ input=open(‘/tmp/spam’,’w’)Create output file(‘w’ means write)
⦁ input=open(‘data’,’r’)Create input file(‘r’ means read)
⦁ Read entire file into a single string
⦁ Read N number of bytes(1 or more)
⦁ S=input.readline() Read next line(through end-line marker)
⦁ L=input.readline() Read entire file into list of line strings
⦁ output.write(S) Write string S onto file
⦁ output.writelines(L) Write all line strings in list L onto file
⦁ output.close() Manual close(or it’s done for you when automatically collected)

Files and Streams:
Coming from a Unix-background, Python treats files as data streams,i.e each file is read and stored as a sequential flow of bytes. Each file has an end-of-file(EOF) marker denoting when the last byte of data has been read from it. This is useful because you can write a program that reads a file in pieces rather than loading the entire file into memory at one time. When a file is read, such as with a readline() method.

Example for End of File:

>>>myfile=open('myfile','w')     #open/create file for input
>>>myfile.write('hello text file')      #write a line of text
>>>myfile=open('myfile','r')       #open for output 'hello text file'
' '                       #empty string denotes end of files

Creating a File:
Creating a file is extremely easy with Python. As shown in the example above, you simply create the variable that will represent the file, open the file, give it a filename, and tell Python that you want to write to it.
⦁ “a”: Appends all output to the end of the file; does not overwrite information currently present. If the indicated file does not exist, it is created.
⦁ “r”:Opens a file for input(reading).If the file does not exist, an IOError exception is raised.
⦁ “r+”: Opens a file for input and output. If the file does not exist, cause an IOError exception.
⦁ “w”:Opens a file for output(writing).If the file exists, it is overwritten. If the file does not exist, one is created.
⦁ “w+”: Opens a file for input and output. If the file exists, it is overwritten; otherwise, one is created.
⦁ “ab”,”rb”,”r+b”,”wb”,”w+b”:Opens a file for binary input or output.

Reading From a File:
If you notice in the above list, the standard read-modes produce an I/O (input/output) error if the file doesn’t exist. If you end up with this error, your program will halt and give you an error message, like below:

>>>file = open("myfile","r")
Traceback(most recent call last):
File"<stdin>", line 1,in <module>
IOError:[Errno 2] No such file or directory:'myfile'

Catching errors,

>>>f.write("hello there,my text file.\n Will you fail gracefully?")
except IOError:
print "The file doesn't exist"

['hello there,my text file.\n','Will you fail gracefully?']

1.Example for Reading of File:

 fileptr = open("file.txt","r"); 
                                                #stores all the data of the file into the variable content 
content =; 
                                                # prints the type of the data stored in the file 
                                                 #prints the content of the file 
                                                 #closes the opened file 
<class 'str'>
Hi, I am 

2.Example for Creating File:

#open the file.txt in read mode. causes error if no such file exists. 
fileptr = open("file2.txt","x"); 
if fileptr: 
print("File created successfully"); 
File created successfully
Translate »