October
11
File Handling
Python has built-in function ‘open’ which open file for read/write operation and returns a file object.
Open method takes three parameter filename, mode of opening file, buffering. Only filename is mandatory others are optional.
Some Important mode of opening file:
1 2 3 |
fd = open("tmp.txt","w") dir(fd) ['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'closed', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines'] |
1 2 3 4 5 6 7 8 9 10 11 12 |
"r" : This is the default mode and file will be open in read mode. The file pointer is placed at the beginning of the file. "rb": Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. "r+": Opens a file for both reading and writing. The file pointer placed at the beginning of the file. "rb+": Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file. "w": Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. "wb": Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. "w+": Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. "wb+": Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. "a": Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. "ab": Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. "a+": Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. "ab+" Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. |
Some important tips while working with file :
- Always remember to read files line by line than reading them as a whole. Sometimes you may have to read files which are way bigger than your available RAM
- try to use with statement. It will take care of closing the file for you
- Always remember to close the file because there is an upper limit to the number of files a program can open