file handling
file handling
E.G. HTML, XML, CSS, CSV, TSV, INI, PY etc E.G. PNG, JPG, ZIP, PDF, MP4,AVI etc
OPEN()
• The method open() is used to open an existing file or creating a new file.
• If the complete directory is not given then the file will be created in the directory in which the
python file is stored.
Syntax: – file_object=open(filename, Access_mode, Buffering)
it returns a file handle which can be stored in the name file_object.
The access mode tells in what mode the file should be opened for operations.
• Reading: denoted by ‘r’ or ‘rb’. USED TO READ DATA FROM THE FILE. The pointer
will be set at the beginning of the file. If the file does not exists, it gives
FILENOTFOUND error.
• Writing: denoted by ‘w’ or ‘wb’ .used for overwriting the information on existing file.
The pointer will be set at the beginning of the file.If the file does not exists, it creates a
new file.
• Append: denoted by ‘a’ or ‘ab’. IT is somewhat similar to the write mode. If the file
does not exists, it creates a new file. But instead of over writing the information it
allows to add the information at the end. The pointer will be set at the end of the file.
Following are the access modes for text file:
• ‘r+’ /’rb+’– Read and Write Mode: This mode is used when we want to write or read
the data from the same file.
• ‘a+’ /’ab+’– Append and Read Mode: This mode is used when we want to read data
from the file or append the data into the same file
Buffering is the process of storing a chunk of a file in a temporary memory until the file
loads completely.
• If the buffering is set to 0 , then no buffering takes place.
• The buffering is set to 1, it assigns a line buffer.
• The buffering is set to value greater than 1, it determines the size of buffer in
bytes.
• By default, the buffering is set to -1(system default size).
While specifying full path to a file in an attempt to open it, take care that it should not form an escape sequence
with \. e,g,
To open a file DATA.TXT, avoid writing F=open(“D:\temp\DATA.TXT”, ”r”).
Instead, use following ways to specify the path:
f = open(r“D:\temp\data.txt”, “r”), which converts the path to raw string.
OR
f = open(“D:\\ temp \\data.txt“, “r”)
CLOSING FILE
To close file in Python, use close() with following syntax:
file_object.close()
Note:
A file object f has following attributes:
a) f.name: display the name of the file opened.
b) f.encoding: returns the type of encoding used.
c) f.mode: returns the access mode.
d) f.newlines: returns the \r\n or \n or None if no newline character is used.
e) f.closed: returns True if the file is closed.
READING TEXT FILES
There are three ways in which we can read the files in python.
read([n]) –returns n bytes read from the file in form of string. If no argument is
provided, it returns the entire content of the file.
readline([n])- returns the content line by line in form of string. If argument n is
provided, it reads and returns n bytes of the content.
readlines() – returns all the lines of file in form of list.
WRITING INTO TEXT FILES
There are two methods for writing data into a file:
write(string) : writes a line of text into the file as string
object.
writelines(list): writes multiple lines of text into the file
as list object.
FLUSH()
When we write any data to file, python hold everything in buffer
(temporary memory) and pushes it onto actual file later. To forcefully
push the content of buffer onto storage, flush() function can be used.
Python automatically flushes the files when closing them i.e. it will be
implicitly called by the close(), BUT if you want to flush before closing
any file you can use flush().
Without flush():
When you run the above code, program will stopped at “Press any key”, for time being don’t press
any key and go to folder where file “temp.txt” is created and open it to see what is in the file till now.
No content appears in the file until the key is pressed and close() is executed.
With flush():
When you run the above code, program will stopped at “Press any key”, for time being don’t press
any key and go to folder where file “temp.txt” is created and open it to see what is in the file till now.
The content HelloWelcome to Disneyland is written into the file due to flush().