FILES ,MODULES, PACKAGES
File is a named location on disk to store related information. It is used to
permanently store data in a non-volatile memory (e.g. hard disk).
Since, random access memory (RAM) is volatile which loses its data when
computer is turned off, we use files for future use of the data.
When we want to read from or write to a file we need to open it first. When
we are done, it needs to be closed, so that resources that are tied with the file are
freed. Hence, in Python, a file operation takes place in the following order.
1. Open a file
2. Read or write (perform operation)
3. Close the file
Opening a file
Python has a built-in function open() to open a file. This function returns a file
object, also called a handle, as it is used to read or modify the file accordingly.
>>> f = open("test.txt") # open file in current directory
>>> f = open("C:/Python33/README.txt") # specifying full path
We can specify the mode while opening a file. In mode, we specify whether we
want to read 'r', write 'w' or append 'a' to the file. We also specify if we want to
open the file in text mode or binary mode.
The default is reading in text mode. In this mode, we get strings when reading from
the file. On the other hand, binary mode returns bytes and this is the mode to be
used when dealing with non-text files like image or exe files.
PYTHON FILE MODES:
Mode : Description
'r' : Open a file for reading. (default)
'w' : Open a file for writing. Creates a new file if it does not exist or truncates the
file if it exists.
'x' : Open a file for exclusive creation. If the file already exists, the operation fails.
,'a' : Open for appending at the end of the file without truncating it. Creates a new
file if it does not exist.
't' : Open in text mode. (default)
'b' : Open in binary mode.
'+' : Open a file for updating (reading and w
f = open("test.txt") # equivalent to 'r' or 'rt'
f = open("test.txt",'w') # write in text mode
f = open("img.bmp",'r+b') # read and write in binary mode
Hence, when working with files in text mode, it is highly recommended to specify
the encoding type.
f = open("test.txt",mode = 'r',encoding = 'utf-8')
Closing a File
When we are done with operations to the file, we need to properly close it.
Closing a file will free up the resources that were tied with the file and is done
using the close() method.
Python has a garbage collector to clean up unreferenced objects but, we must not
rely on it to close the file.
f = open("test.txt",encoding = 'utf-8')
# perform file operations
f.close()
This method is not entirely safe. If an exception occurs when we are performing
some operation with the file, the code exits without closing the file. A safer way is
to use a try...finally block.
try:
f= open("test.txt",encoding = 'utf-8')
# perform file operations
finally:
, f.close()
This way, we are guaranteed that the file is properly closed even if an
exception is raised, causing program flow to stop.
The best way to do this is using the with statement. This ensures that the file is
closed when the block inside with is exited.
We don't need to explicitly call the close() method. It is done internally.
with open("test.txt",encoding = 'utf-8') as f:
# perform file operations
READING AND WRITING:
A text file is a sequence of characters stored on a permanent medium like a hard
drive, flash memory, or CD-ROM.
To write a file, you have to open it with mode 'w' as a second parameter:
>>> fout = open('output.txt', 'w')
>>> print fout
<open file 'output.txt', mode 'w' at 0xb7eb2410>
If the file already exists, opening it in write mode clears out the old data and starts
fresh, so be careful! If the file doesn’t exist, a new one is created.
The write method puts data into the file.
>>> line1 = "This here's the wattle,\n"
>>>fout.write(line1)
Again, the file object keeps track of where it is, so if you call write again, it adds
the new data to the end.
>>> line2 = "the emblem of our land.\n"
>>> fout.write(line2)
When you are done writing, you have to close the file.
>>> fout.close()