You are on page 1of 3

Open file

Python has a number of built-in functions to read and write information to a file on your
computer.

open() returns a file object, and is most commonly used with two
arguments: open(filename, mode):

f = open('somefile.txt', 'w')

The first argument is a string containing the filename. The second argument is another string
containing a few characters describing the way in which the file will be used. It can be 'r' if
the file will only be read, 'w' – for writing only (an already existing file with the same name
will be erased), and 'a' opens the file for appending – any data written to the file is added to
its end. 'r+' opens the file for both reading and writing. The mode argument is
optional; 'r' will be assumed if it’s omitted.

It is good practice to use the with keyword when dealing with file objects. The advantage is
that the file is properly closed after the code suite finishes.

with open('somefile.txt') as f:
read_data = f.read()

# We can check that the file has been automatically closed.


f.closed
True

Important: If you’re not using the with keyword, then you should call f.close() to close
the file and free up any system resources used by it. You cannot use the file object after it is
closed, whether by a with statement or by calling f.close().

In the code editor, open the file input1.txt in read mode properly using
the with statement. Check out the name that is used for the file on the next line and use it.
Afterwards, close the output file outfile that was opened.

After running your code, check out the output file that appeared in the course view among
the other files.

Hint
Supply the r argument to the method open(), just for the sake of practicing!
Read file
To read a file’s contents, you can call f.read(size), which reads some quantity of data and
returns it as a string. When size is omitted or negative, the entire contents of the file will be
read and returned.

with open('somefile.txt') as f:
print(f.read())
Here's everything that's in the file.\n

Note: there will be a problem if the file is twice as large as your machine’s memory.

f.readline() reads a single line from the file; a newline character (\n) is left at the end of
the string and is only omitted on the last line of the file if the file doesn’t end in a newline.
If f.readline() returns an empty string, the end of the file has been reached, while a blank
line is represented by \n, a string containing only a single newline.

f.readline()
'This is the first line of the file.\n'
f.readline()
'Second line of the file\n'
f.readline()
''

For reading lines from a file, you can loop over the file object. This is memory efficient, fast,
and makes the code simple:

for line in f:
print(line)
This is the first line of the file.
Second line of the file

If you want to read all the lines of a file in a list, you can also use list(f) or
f.readlines().

For more details, check out the section Methods of File Objects in Python Tutorial.

Print the contents of "input.txt" to output by iterating over the lines of the file and printing
each one. Then print only the first line of "input1.txt".

You might also like