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

While handling is an essential

part of learning Python. Python has several built in functions


to create and manipulate files. Fire handling includes opening,
reading and writing files amongst other operations. As a developer, you'll probably
work with large amounts of data and far handling makes that easier. This is why
it's important to
learn how to work with files. Whether you're working with
data on your computer, on the web or in the cloud, it will most
likely be saved in some type of file. There are two file handling
functions in Python, open and close. Let's explore the open function first. The
open function is used for
reading, writing and creating files. The open function accepts two arguments. The
first is the file name and
or the file location and the second argument is the mode. The mode indicates what
action is required
such as reading, writing or creating. It's also specifies if you want the file
output in text or binary format. Let's explore the modes of file
handling you can use in Python. First you have r which is used to open and
read a file in text format and to rb opens and
reads a file in binary format. You will learn more about this later. R+ on the
other hand, opens the file for
both reading and writing and w opens the file for writing. Note that w will
overwrite
the existing file. Lastly, a opens files for
editing or appending data. Next, there's the close function. The close function is
used for
closing the open file connection, know that it does not take any arguments. There
is one more way to open and
close a file in Python and that's with open function. The advantage of using it is
that
it closes the file automatically. The with open function will
be demonstrated shortly. By now you understand how to open files
for certain actions, but you might be wondering what the difference is between
opening a file in text and binary formats. In Python, you generally handle files in
two ways, either in text or binary format. The text format is more user
friendly because humans can read. You won't be able to read
files in binary formats, but they are much more compact and
therefore result in better performance. Now let's cover how to specify
the type of file handling in Python. Python uses text as the default format for
file handling. So just passing in any mode for reading or writing a file will
automatically
set it to a text format. To set the file handling to binary, you need to pass the
letter b along
with either the read or write option. For example, you write open
the file name and rb to read a file in binary format or ab to append or
add to a file in binary format. You'll now explore file handling in code. First I
declare a simple
variable called file and assign the open function to
it to gain access to a file. But before I can use the open function,
I first need to create a new file for testing, let's call it test.txt. Inside this
text file I
add a simple line of text. Hello there, good. Let's go back to the Python file.
Inside the parentheses of the open
function, I can now add the first argument, namely test.txt between
quotation marks since it's a string. For the second argument,
I type in the word mode, equal sign and then I will just use r for
read also between quotation marks. So far, the variable called file will have
access to the contents of the test.txt. But to actually read the file, you need to
add a read line or read lines function. Read line will just return the first line
of the file while read lines will output an array with multiple lines. Since we
only have a single
line of text in this file, I'll just use the read line
function I type file.readline(). I assigned the line of code
to a new variable name, data. Then, I add a print statement
to print the contents of data. Lastly, I add a close function that will
close access to the last text.txt file. I simply close with a set of parentheses. I
click on run and the content of the file
is printed out, namely, Hello there. Next, I'll demonstrate another way
to gain access to a file in Python. I'll change the open function
to the with open function. I'll just clear the screen, to assign
a variable to the with open function. You need to add as after the parentheses
and then the variable name. Why would you use that with open function? The with
open function is better
at exception handling and will automatically close the file for you. Just like
before, I create a second
variable data, read line and then print the contents of data. I'll click on run and
just like before
the contents of the file are printed. You've covered how to work
with files in Python. This includes the built in functions
to create and manipulate files and the functions to open,
read and write files.

You might also like