Python's built-in functions input() and print() perform read/write operations with standard IO streams. The input() function reads text into memory variables from keyboard which is defined as sys.stdin and the print() function send data to display device identified as sys.stdout. The sys module presents definitions of these objects.
Instead of standard output device, if data is saved in persistent computer files then it can be used subsequently. File is a named location in computer’s non-volatile storage device such as disk. Python's built-in function open() returns file object mapped to a file on the permanent storage like disk.
File object is returned by open() function which needs name of file along with its path and file opening mode.
file = open(name, mode)
The mode parameter decides how the file is to be treated. Default mode is 'r' which means that it is now possible to read data from the file. To store data in it, mode parameter should be set to ‘w’. Other supported values of mode parameter and their significance are listed in following table:
character | purpose |
---|---|
r | Opens a file for reading only. (default) |
w | Opens a file for writing only, deleting earlier contents |
a | Opens a file for appending. |
t | opens file in text format (default) |
b | Opens a file in binary format. |
+ | Opens a file for simultaneous reading and writing. |
x | opens file for exclusive creation. |
The open() function returns a file like object representing any stream such a file, byte stream, socket or pipe etc. The file object supports various methods for operations on underlying data stream.
Following statement opens python.txt in write mode.
>>> f=open("python.txt","w")
Next we have to put certain data in the file. The write() method stores a string in the file.
>>> f.write(("Monty Python's Flying Circus")
Make sure that you close the file object in the end.
>>> f.close()
The "python.txt" is now created in current folder. Try opening it using any text editor to confirm that it contains above text.
The file object also has writelines() method to write items in a list object to a file. The newline characters ("\n) should be the part of the string.
lines=[" Beautiful is better than ugly.\n", "Explicit is better than implicit.\n", "Simple is better than complex.\n", "Complex is better than complicated.\n"]
f=open("python.txt","w") f.writelines(lines) f.close()
The python.txt shows following data. When opened with editor.
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated.
To read data from a file, we need to open it in ‘r’ mode.
>>> f=open('python.txt','r')
The read() method reads certain number of characters from file's current reading position. To read first 9 characters in file:
>>> f.read(9) 'Beautiful'
This method reads current line till it encounters newline character.
>>> f=open('python.txt','r') >>> f.readline() 'Beautiful is better than ugly.\n'
To read file line by line until all lines are read,
f=open("python.txt","r") while True: line=f.readline() if line=='':break print (line) f.close()
This method reads all lines and returns a list object.
>>> f=open('python.txt','r') >>> f.readlines() ['Beautiful is better than ugly.\n', 'Explicit is better than implicit.\n', 'Simple is better than complex.\n', 'Complex is better than complicated.\n']
File operation is subject to occurrence of exception. If file could not be opened, OSError is raised and if it is not found, FileNotFoundError is raised.
>>> f=open("anyfile.txt","r") Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> f=open("anyfile.txt","r") FileNotFoundError: [Errno 2] No such file or directory: 'anyfile.txt'
Hence such operations should always be provided exception handling mechanism.
try: f=open("python.txt","r") while True: line=f.readline() if line=='':break print (line, end='') except FileNotFoundError: print ("File is not found") else: f.close()
The file object is a data stream that supports next() method to read file line by line. When end of file is encountered, StopIteration exception is raised.
f=open("python.txt","r") while True: try: line=next(f) print (line, end="") except StopIteration: break f.close()
Output:
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated.
When a file is opened with "w" mode its contents are deleted if it exists already. In order to add more data to existing file use "a" mode (append mode).
f=open("python.txt","a")
If additional data is now written, it gets added to the end of file.
>>> f=open('python.txt','a') >>> f.write("Flat is better than nested.\n") 28 >>> f.close()
The file will have additional line at the end.
"w" mode or "a" mode allows data to be written into and cannot be read from. On the other hand "r" mode allows reading only but doesn't allow write operation. In order to perform simultaneous read/write operations, use "r+" or "w+" mode.
The seek() method of file object sets current read/write position to specified location in the file.
f.seek(offset, whence)
Here the whence parameter counts offset from beginning, current position or end respectively:
Let us assume that python.txt contains following lines.
Beautiful is better than ugly. Explicit is better than implicit.
We now try to replace the word 'better' with 'always better'. First open the file in read/write mode. Place the file pointer at the beginning of 2nd line and read it. The replace 'better' with 'always better' and rewrite the line.
f=open("python.txt","r+") f.seek(32,0) s=f.readline() s=s.replace('is', 'is always') print (s) f.seek(32,0) f.write(s) f.seek(0,0) lines=f.readlines() for line in lines: print (line) f.close()
Output:
Beautiful is better than ugly. Explicit is always better than implicit.
The open() function opens a file in text format by default. To open file in binary format add ‘b’ to mode parameter. Hence "rb" mode opens file in binary format for reading and "wb" mode opens file in binary format for writing. Unlike text mode files, binary files are not human readable. When opened using any text editor, the data is unrecognizable.
Following code stores a number in a binary file. It is first converted in a bytes before writing. The function to_bytes() of int class returns byte representation of the object.
f=open('number','wb') d=1024 f.write(int.to_bytes(d,16, 'big')) f.close()
To read above binary file, output of read() method is casted to integer by from_bytes() method.
f=open('number','wb') d=1024 f.write(int.to_bytes(d,16, 'big')) f.close()
In addition to above methods, file object is also characterized by following attributes:
Attribute | Description |
---|---|
file.closed | Returns true if file is closed, false otherwise. |
file.mode | Returns access mode with which file was opened. |
file.name | Returns name of the file. |
This is my first time here. I am truly impressed to read all this in one place.
Thank you for your wonderful codes and website, you helped me a lot especially in this socket module. Thank you again!
Thank you for taking the time to share your knowledge about using python to find the path! Your insight and guidance is greatly appreciated.
Usually I by no means touch upon blogs however your article is so convincing that I by no means prevent myself to mention it here.
Usually, I never touch upon blogs; however, your article is so convincing that I could not prevent myself from mentioning how nice it is written.
C# is an object-oriented programming developed by Microsoft that uses ...
Leave a Reply
Your email address will not be published. Required fields are marked *