Unit IV - Files - MODIFIED
Unit IV - Files - MODIFIED
FILE HANDLING
File
A file is a collection of data stored on a secondary storage device like hard disk. A file is basically
used because real-life applications involve large amounts of data and in such situations the console
• First, it becomes cumbersome and time consuming to handle huge amount of data through
terminals.
• Second, when doing I/O using terminal, the entire data is lost when either the program is
terminated or computer is turned off. Therefore, it becomes necessary to store data on a permanent
storage (the disks) and read whenever necessary, without destroying the data.
2
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
File Path
Besides this, other character conversions may also be done to satisfy the
• A binary file is a file which may contain any type of data, encoded in
binary form for computer storage and processing purposes.
• It includes files such as word processing documents, PDFs, images,
spreadsheets, videos, zip files and other executable programs.
• Like a text file, a binary file is a collection of bytes. A binary file is also
referred to as a character stream with following two essential
differences.
8
© Oxford University Press 2017. All rights reserved.
BINARY FILES
• A binary file does not require any special processing of the data
and each byte of data is transferred to or from the disk
unprocessed.
Example:
10
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
The open() Function – Access Modes
12
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
The File Object Attributes
Once a file is successfully opened, a file object is returned. Using this file object,
you can easily access different type of information related to that file. This
information can be obtained by reading values of specific attributes of the file.
14
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
The close () Method
• The close() method is used to close the file object. Once a file object
is closed, you cannot further read from or write into the file
associated with the file object.
• While closing the file object the close() flushes any unwritten
information. Although, Python automatically closes a file when the
reference object of a file is reassigned to another file, but as a good
programming habit you should always explicitly use the close()
method to close a file. The syntax of close() is fileObj.close()
16
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
The close() method frees up any system resources such as file
descriptors, file locks, etc. that are associated with the file. Moreover,
there is an upper limit to the number of files a program can open. If that
limit is exceeded then the program may even crash or work in
unexpected manner. Thus, you can waste lots of memory if you keep
many files open unnecessarily and also remember that open files
always stand a chance of corruption and data loss.
Once the file is closed using the close() method, any attempt to use
• Once you have stored some data in a file, you can always open that file again to
write more data or append data to it.
• To append a file, you must open it using 'a' or 'ab' mode depending on whether it
is a text file or a binary file. Note that if you open a file in 'w' or 'wb' mode and
then start writing data into it, then its existing contents would be overwritten. So
always open the file in 'a' or 'ab' mode to add more data to existing data stored in
the file.
• Appending data is especially essential when creating a log of events or combining
a large set of data into one file.
25
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
APPEND
APPEND VS WRITE
The read() and readline() Methods
The read() method is used to read a string from an already opened file. As said before,
the string can include, alphabets, numbers, characters or other symbols. The syntax of
read() method is fileObj.read([count])
In the above syntax, count is an optional parameter which if passed to the read()
method specifies the number of bytes to be read from the opened file. The read()
method starts reading from the beginning of the file and if count is missing or has a
negative value then, it reads the entire contents of the file (i.e., till the end of file).
The readlines() method is used to read all the lines in the file
29
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Why [] In W+ Mode?
Opening Files using “with” Keyword
It is good programming habit to use the with keyword when working with file objects. This has the
advantage that the file is properly closed after it is used even if an error occurs during read or write
operation or even when you forget to explicitly close the file.
Example
s:
32
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Splitting Words
Python allows you to read line(s) from a file and splits the line (treated as a string)
based on a character. By default, this character is space but you can even specify any
other character to split words in the string.
35
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
TRY OUT
TRY OUT
Some Other Useful File Methods
41
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
File Positions
• With every file, the file management system associates a pointer often known as
file pointer that facilitate the movement across the file for reading and/ or writing
data.
• The file pointer specifies a location from where the current read or write operation
is initiated. Once the read/write operation is completed, the pointer is
automatically updated.
• Python has various methods that tells or sets the position of the file pointer.
• For example, the tell() method tells the current position within the file at which the next read
or write operation will occur. It is specified as number of bytes from the beginning of the file.
When you just open a file for reading, the file pointer is positioned at location 0, which is the
beginning of the file.
43
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
File Positions
• The seek(offset[, from]) method is used to set the position of the file pointer or
in simpler terms, move the file pointer to a new location. The offset argument
indicates the number of bytes to be moved and the from argument specifies the
reference position from where the bytes are to be moved.
f.seek(offset, from_what), where f is file pointer
Offset: Number of positions to move forward
from_what: It defines point of reference.
Returns: Return the new absolute position.
44
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
File Positions - Example
45
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Renaming and Deleting Files
• The os module in Python has various methods that can be used to perform file-
processing operations like renaming and deleting files. To use the methods
defined in the os module, you should first import it in your program then call any
related functions.
• The rename() Method: The rename() method takes two arguments, the current filename and the
new filename. Its syntax is:
• os.rename(old_file_name, new_file_name)
• The remove() Method: This method can be used to delete file(s). The method takes a filename
(name of the file to be deleted) as an argument and deletes that file. Its syntax is:
• os.remove(file_name)
48
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Renaming and Deleting Files
49
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Directory Methods
• The mkdir() Method: The mkdir()method of the OS module is used to create
directories in the current directory. The method takes the name of the directory (the
one to be created) as an argument. The syntax of mkdir() is,
os.mkdir("new_dir_name")
• The getcwd() Method: The getcwd() method is used to display the current working
directory (cwd).
os.getcwd()
• The chdir() Method: The chdir() method is used to change the current directory. The
method takes the name of the directory which you want to make the current
directory as an argument. Its syntax is 50
© Oxford University Press 2017. All rights reserved.
Directory Methods
52
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Methods from the os Module
54
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Methods from the os Module
• The os.path.split(path) Method: This method accepts a file path and returns its
directory name as well as the . So it is equivalent to using two separate methods
os.path.dirname() and os.path.basename()
• The os.path.getsize(path) Method: This method returns the size of the file specified
in the path argument.
• The os.listdir(path) Method: The method returns a list of filenames in the specified
path.
• The os.path.exists(path) Method: The method as the name suggests accepts a path
as an argument and returns True if the file or folder specified in the path exists and
False otherwise. 56
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Methods from the os Module
59
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Methods from the os Module — Examples
60
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.