Chapter 5
Chapter 5
Chapter 5
File operation
Declaration
These slides are made for UIT, BU students only. I am not
holding any copy write of it as I had collected these study
materials from different books and websites etc. I have not
mentioned those to avoid complexity.
Opening a file refers to getting the file ready either for reading or for
writing.
This can be done using the open() function.
This function returns a file object and takes two arguments, one
that accepts the file name and another that accepts the
mode(Access Mode).
Now, the question arises what is the access mode? Access modes
govern the type of operations possible in the opened file. It refers to
how the file will be used once it’s 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.
Read Only (‘r’): Open text file for reading. The handle is
positioned at the beginning of the file. If the file does not exist,
raises an I/O error. This is also the default mode in which the file
is opened.
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.
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.
Write and Read (‘w+’): Open the file for reading and writing. For
existing files, data is truncated and over-written. The handle is
positioned at the beginning of the file.
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.
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.
Read Only in Binary format(‘rb’): It lets the user open the file
for reading in binary format.
Read and Write in Binary Format(‘rb+’): It lets the user open
the file for reading and writing in binary format.
Write Only in Binary Format(‘wb’): It lets the user open the file
for writing in binary format. When a file gets opened in this mode,
there are two things that can happen mostly. A new file gets
created if the file does not exist. The content within the file will
get overwritten if the file exists and has some data stored in it.
Write and Read in Binary Format(‘wb+’): It lets the user open
the file for reading as well as writing in binary format. When a file
gets opened in this mode, there are two things that can mostly
happen. A new file gets created for writing and reading if the file
does not exist. The content within the file will get overwritten if
the file exists and has some data stored in it.
The open command will open the file in the read mode and the
for loop will print each line present in the file.
open("file.txt", "w"): This line opens the file "file.txt" in write mode ("w"). If
the file doesn't exist, it will be created. If it does exist, its previous
content will be completely overwritten.
with open("file.txt", "w") as f:: This line starts a context using a with
statement. It ensures that the file is properly closed after writing. This is
important because it not only saves your changes but also releases the
system resources associated with the file.
f.write("Hello World!!!"): Within the context, this line writes the string
"Hello World!!!" to the file. After executing this line, the file will contain
only the text "Hello World!!!".
# Creating a file
file1 = open("myfile.txt", "w")
L = ["This is Delhi \n", "This is Paris \n", "This is London \n"]
file1.seek(0)
file1.seek(0)
file1.seek(0)
# Creating a file
with open("myfile.txt", "w") as file1:
# Writing data to a file
file1.write("Hello \n")
file1.writelines(L)
file1.close() # to change file access modes
# Opening a file
file1 = open('myfile.txt', 'w')
L = ["This is Delhi \n", "This is Paris \n", "This is London \n"]
s = "Hello\n"
# Append-adds at last
file1 = open("myfile.txt", "a") # append mode
file1.write("Today \n")
file1.close()
# Write-Overwrites
file1 = open("myfile.txt", "w") # write mode
file1.write("Tomorrow \n")
file1.close()
Questions?