Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Absolute and Relative Paths

Path of a file is a sequence of the directories to access that file on a computer.


Absolute Path is the full path starting from the root directory to locate a particular file in a
computer system. Example of an absolute path is:
‘E:\ClassXII\SectionA\test.py’
Relative Path is the path of the file relative to the current working directory. For example,with
‘SectionA’ as current working directory the relative path of test.py will be
‘.\test.py’.

Operations related to Text files


I. File Opening: Files can be opened in python by using the open( ) method as
follows:
<file_obj> = open (‘<file_path>’,’access_mode’)

Here <file_obj> is the file handle which provides the access to the physical file stored
on a computer. The file object is also called the file pointer. It accesses the contents of the file
in a sequential manner.

All the operations can be performed on the file only through the file pointer.

The open( ) method accepts one required argument which is the path of the file to opened. The
second argument ‘access_mode’ is optional and defines the operations that can be performed on
the file once it is opened. The default access mode is ‘read mode’.

Note: While specifying the path of the file, one must either use ‘\’ to escape all the special
characters in the path or make it a raw string by adding ‘r’ in front of the path string.
Different access modes are described in Table 1. For example, in order to open a text file
named ‘myfile.txt’, the following syntax will be used:

f = open (r’D:\MyFolder\myfile.txt’, ‘r’)

Access modes govern the type of operations possible in the opened file. It refers to how the file
will be used once its opened. These modes also define the location of the File Handle in the
file. File handle is like a cursor, which defines from where the data has to be read or written in
the file. There are 6 access modes in python.

1. Read Only (‘r’): Open text file for reading. The handle is positioned at the beginning of the
file. If the file does not exists, raises the I/O error. This is also the default mode in which a
file is opened.
2. Read and Write (‘r+’): Open the file for reading and writing. The handle is positioned at
the beginning of the file. Raises I/O error if the file does not exist.
3. Write Only (‘w’) : Open the file for writing. For the existing files, the data is truncated and
over-written. The handle is positioned at the beginning of the file. Creates the file if the file
does not exist.
4. Write and Read (‘w+’) : Open the file for reading and writing. For an existing file, data is
truncated and over-written. The handle is positioned at the beginning of the file.
5. Append Only (‘a’): Open the file for writing. The file is created if it does not exist. The
handle is positioned at the end of the file. The data being written will be inserted at the
end, after the existing data.
6. Append and Read (‘a+’) : Open the file for reading and writing. The file is created if it
does not exist. The handle is positioned at the end of the file. The data being written will be
inserted at the end, after the existing data.
File Reading: Once the file is opened using the open( ) function, file reading can be
performed by using any of the following three functions:

a. f.read([n]): This function accepts an optional integer argument ‘n’ and returns nbytes
from the file. If ‘n; is not specified by the user, then it reads all the contents of the file at
once and returns in string format. The examples are as follows:

Myfile
I am groot.
CODE OUTPUT
f=open(r’MyFile’,’r’) I am groot.
data = f.read( )
print(data)
f.close( )

f=open(r’MyFile’,’r’) I am
data = f.read(4 )
print(data)
f.close( )

f.readline([n]): This function accepts an optional integer argument and returns n bytes
after reading from the file. If ‘n’ is not specified, then it reads only one line and returns it in string
format. The examples are as follows:

Myfile
I am groot.
I am Peter.
How are you?

f.readlines( ): This function does not accept any argument. It reads all the content of the
text file at once and returns a list containing all the lines as individual elements. The
examples are as follows:
Note: f is the file pointer. All the operations will be performed in the file referenced by f.

File Writing: For writing content into text files in python, the file must be opened
using the ‘w’ or ‘a’ access mode. File writing can be performed by using following two
functions:
f.write(str): This function accepts the string str and writes this string into the file.
The examples of the f.write(str) function are as follows:

Myfile
I am groot.
I am Peter.
How are you?

Note: Opening file in write mode overwritten the file contents,so be carefull. Use append
mode in such situations.

f.writelines(lst): This function accepts multiple lines in the form of a list and writes
all the lines into the file. The examples are:

Closing a file: One must always close a file when the file is no longer required for
any more operations. The file can be closed by using the close( ) function which can be used
with the file pointer as follows:
<file_handle>.close( )

You might also like