0

In python's OS module there is a method to open a file and a method to read a file.

The docs for the open method say:

Open the file file and set various flags according to flags and possibly its mode according to mode. The default mode is 0777 (octal), and the current umask value is first masked out. Return the file descriptor for the newly opened file.

The docs for the read method say;

Read at most n bytes from file descriptor fd. Return a string containing the bytes read. If the end of the file referred to by fd has been reached, an empty string is returned.

I understand what it means to read n bytes from a file. But how does this differ from open?

4 Answers 4

7

"Opening" a file doesn't actually bring any of the data from the file into your program. It just prepares the file for reading (or writing), so when your program is ready to read the contents of the file it can do so right away.

Sign up to request clarification or add additional context in comments.

Comments

3

Opening a file allows you to read or write to it (depending on the flag you pass as the second argument), whereas reading it actually pulls the data from a file that is typcially saved into a variable for processing or printed as output.

You do not always read from a file once it is opened. Opening also allows you to write to a file, either by overwriting all the contents or appending to the contents.

To read from a file:

>>> myfile = open('foo.txt', 'r')
>>> myfile.read()

First you open the file with read permission (r) Then you read() from the file

To write to a file:

>>> myfile = open('foo.txt', 'r')
>>> myfile.write('I am writing to foo.txt')

The only thing that is being done in line 1 of each of these examples is opening the file. It is not until we actually read() from the file that anything is changed

2 Comments

Strictly speaking opening a file gives you a handle to it. It does not allow you read it (you may be able to open a file, but only write to it). A small but significant difference.
True, I will fix that. I was only thinking about reading when I wrote this
0

open gets you a fd (file descriptor), you can read from that fd later.

One may also open a file for other purpose, say write to a file.

Comments

0

It seems to me you can read lines from the file handle without invoking the read method but I guess read() truly puts the data in the variable location. In my course we seem to be printing lines, counting lines, and adding numbers from lines without using read().

The rstrip() method needs to be used, however, because printing the line from the file handle using a for in statement also prints the invisible line break symbol at the end of the line, as does the print statement.

From Python for Everybody by Charles Severance, this is the starter code.

  """
7.2
Write a program that prompts for a file name,
then opens that file and reads through the file,
looking for lines of the form:
X-DSPAM-Confidence:    0.8475
Count these lines and extract the floating point
values from each of the lines and compute the
average of those values and produce an output as
shown below. Do not use the sum() function or a
variable named sum in your solution.
You can download the sample data at
http://www.py4e.com/code3/mbox-short.txt when you
are testing below enter mbox-short.txt as the file name.
"""


# Use the file name mbox-short.txt as the file name

fname = input("Enter file name: ") 
fh = open(fname) 
for line in fh:
    if not line.startswith("X-DSPAM-Confidence:") : 
        continue
    print(line)
print("Done")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.