Lecture 10 File Operations
Lecture 10 File Operations
Access modes:
➢ "r" - this is the default, with this you open the file for reading.
➢ "w" - this is for writing
➢ "a" - this is for appending to the end of the file
➢ "b" - this stands for binary format and is useful when playing around with binary files
like photos,pdfs,executables;it should be used for all files that dont contain text.
➢ "x" - exclusive creation.Means that you exclusively create a file and it fails if the file
already exists.this is the same as using "w" access mode , only it raises an exception
if the file already exists.
Python 3 Files - Opening & Reading - read()
➢ Python provides three important methods to read data from a file.
➢ The first method is read(), which returns the entire content of a file, in the form of a
string
Python 3 Files - Opening & Reading - readline()
➢ Another useful method is readline(). This returns the file content, line by line, one line
at a time, each time you call the method
Python 3 Files - Opening & Reading - readlines()
➢ The third way of reading files is using the readlines() method; this method returns a
list, where each element of the list is a line in the file; this method is very useful for
iterating over a file and it is frequently used when working with files.
Python 3 Files - Opening & Reading - readlines()
Python 3 Files - Opening & Reading - “x” access mode
Let's also test the "x" mode on our already existing file and see if it really generates an
exception, since the routers.txt file already exists.
Python 3 Files - Writing & Appending
➢ First of all, I should say that the "w" method also creates the file for writing,if the file
doesn't exist and overrides the file, if the file already exists.
➢ Now, let's check that our text was indeed written to the file.
➢ You may say "well, let's use the read() method for this". Actually, I'm sorry, but you
cannot do this, because now we have the file open only for writing, not for writing and
reading.
Python 3 Files - Writing & Appending
➢ So, we got an error saying that the file is not readable. Don't worry, I will show you
how to open a file for both reading and writing, but, for now, let’s check if our string
was indeed added to the file, by simply opening it.
➢ Uhm, disappointment again! There's no text in the file. Why is that? The answer is
because we didn't close the file after writing to it and thus our changes were
not saved
Python 3 Files - Writing & Appending
➢ When you open an existing file for writing, the content of the file is deleted and
Python overwrites the old data with the new data you input using the write() method.
➢ Be very careful when using the open() function and the "w" access method!
Python 3 Files - Writing & Appending – “writelines”
➢ Notice that the elements of the list which are passed as an argument are written to
the file, without any spaces or newlines in between them. This is how the writelines()
method works.
Python 3 Files - Writing & Appending – “a”
➢ Now, what if you want to keep the data already written to the file and also write some
new data at the end of the file? In that case, you should use the a access method.
➢ This stands for appending. This access mode adds new data to the end of the file, if
that file exists. If it doesn't, then it creates the file
Python 3 Files - Writing & Appending – “r+, w+, a+”
➢ You can open a file for both writing and reading at the same time, to be able to read
its contents immediately after writing.
➢ The access methods to use in this case are: r+, w+ and a+.
➢ "w+" will open a file for writing and reading at the same time and, if the file doesn't
exist, it will create it.
Python 3 Files - Closing. The "with" Statement
➢ Another way to close a file is using the with … as statement.
➢ Using this statement, you don't have to worry about closing the file manually using
the close() method. Instead, the file is closed automatically.
➢ Also, the with … as statement creates a new file object and a new file, if you use the
"w" access method.