Read the Same File Again in Python
Opening files and reading from files
How to opening files and read from files and avert annoying mistakes when reading files
Summary
Opening files and reading their data is something nosotros learn how to practice with a simple double-click in our earliest interactions with computers. However, at the programmatic layer, things are essentially more complicated…
The basic blueprint of opening and reading files in Python
Here's the official Python documentation on reading and writing from files. Just before reading that, let's dive into the bare minimum that I want you lot to know.
Allow'due south just go straight to a code example. Pretend you take a file named example.txt in the current directory. If you don't, merely create one, and then fill it with these lines and save it:
how-do-you-do world and now I say goodbye
Here'southward a short snippet of Python lawmaking to open that file and print out its contents to screen – note that this Python code has to exist run in the same directory that the example.txt
file exists in.
myfile = open ( "case.txt" ) txt = myfile . read () print ( txt ) myfile . shut ()
Did that seem too complicated? Here'southward a less verbose version:
myfile = open ( "example.txt" ) print ( myfile . read ()) myfile . close ()
Here's how to read that file, line-by-line, using a for-loop:
myfile = open ( "example.txt" ) for line in myfile : print ( line ) myfile . close ()
(Note: If you're getting a FileNotFoundError already – that's almost to be expected. Proceed reading!)
Still seem too complicated? Well, there's no getting around the fact that at the programmatic layer, opening a file is distinct from reading its contents. Non simply that, nosotros besides have to manually close the file.
Now let's take this pace-by-step.
How to open a file – an interactive exploration
To open a file, we simply employ the open()
method and pass in, as the commencement statement, the filename:
myfile = open ( "case.txt" )
That seems like shooting fish in a barrel plenty, and so permit'southward jump into some mutual errors.
How to mess up when opening a file
Here is likely the most common fault you'll become when trying to open a file.
FileNotFoundError: [Errno 2] No such file or directory: 'SOME_FILENAME'
In fact, I've seen students waste matter dozens of hours trying to get past this error bulletin, considering they don't stop to read it. So, read it: What does FileNotFoundError
mean?
Endeavor putting spaces where the capitalization occurs:
File Not Establish Mistake
You lot'll get this error because you tried to open a file that simply doesn't exist. Sometimes, it's a elementary typo, trying to open up()
a file named "case.txt"
but accidentally misspelling it as "exmple.txt"
.
Just more than often, it'southward considering you know a file exists under a given filename, such as "example.txt"
– merely how does your Python code know where that file is? Is it the "example.txt"
that exists in your Downloads binder? Or the one that might exist in your Documents folder? Or the thousands of other folders on your estimator system?
That'south a pretty complicated question. Merely the kickoff stride in non wasting your fourth dimension is that if you ever encounter this error, cease whatever else yous are doing. Don't tweak your convoluted for-loop. Don't try to install a new Python library. Don't restart your calculator, then re-run the script to encounter if the error magically fixes itself.
The error FileNotFoundError
occurs because you either don't know where a file really is on your computer. Or, even if you do, you don't know how to tell your Python program where it is. Don't try to fix other parts of your code that aren't related to specifying filenames or paths.
How to prepare a FileNotFoundError
Here's a surefire fix: make certain the file actually exists.
Let's start from scratch by making an error. In your system crush (i.e. Concluding), change to your Desktop binder:
$ cd ~/Desktop
Now, run ipython:
$ ipython
And now that you're in the interactive Python interpreter, attempt to open a filename that you lot know does not exist on your Desktop, and then enjoy the error bulletin:
>>> myfile = open ( "whateverdude.txt" )
--------------------------------------------------------------------------- FileNotFoundError Traceback (almost contempo call last) <ipython-input-1-4234adaa1c35> in <module>() ----> 1 myfile = open("whateverdude.txt") FileNotFoundError: [Errno two] No such file or directory: 'whateverdude.txt'
Now manually create the file on your Desktop, using Sublime Text iii or any yous want. Add some text to it, then save information technology.
this is my file
Wait and meet for yourself that this file actually exists in your Desktop folder:
OK, now switch back to your interactive Python shell (i.e. ipython), the 1 that you opened afterwards changing into the Desktop folder (i.east. cd ~/Desktop
). Re-run that open up()
control, the one that resulted in the FileNotFoundError:
>>> myfile = open ( "whateverdude.txt" )
Hopefully, yous shouldn't get an fault.
Merely what is that object that the myfile
variable points to? Use the type()
method to figure it out:
>>> type ( myfile ) _io . TextIOWrapper
And what is that? The details aren't important, other than to point out that myfile
is most definitely non just a string literal, i.e. str
.
Utilize the Tab autocomplete (i.e. blazon in myfile.
) to get a list of existing methods and attributes for the myfile
object:
myfile.buffer myfile.isatty myfile.readlines myfile.close myfile.line_buffering myfile.seek myfile.closed myfile.style myfile.seekable myfile.disassemble myfile.name myfile.tell myfile.encoding myfile.newlines myfile.truncate myfile.errors myfile.read myfile.writable myfile.fileno myfile.readable myfile.write myfile.flush myfile.readline myfile.writelines
Well, we can practise a lot more than with files than only read()
from them. Only let's focus on just reading for now.
How to read from a file – an interactive exploration
Assuming the myfile
variable points to some kind of file object, this is how y'all read from it:
>>> mystuff = myfile . read ()
What'southward in that mystuff
variable? Again, use the type()
part:
>>> blazon ( mystuff ) str
It's just a string. Which means of course that nosotros can impress it out:
>>> print ( mystuff ) this is my file
Or count the number of characters:
>>> len ( mystuff ) fifteen
Or print it out in all-caps:
>>> print ( mystuff . upper ()) THIS IS MY FILE
And that'southward all in that location's to reading from a file that has been opened.
At present onto the mistakes.
How to mess up when reading from a file
Here's a very, very common error:
>>> filename = "example.txt" >>> filename . read ()
The mistake output:
AttributeError Traceback (most recent call terminal) <ipython-input-9-441b57e838ab> in <module>() ----> ane filename.read() AttributeError: 'str' object has no aspect 'read'
Accept careful note that this is not a FileNotFoundError. It is an AttributeError – which, admittedly, is not very clear – but read the side by side function:
'str' object has no attribute 'read'
The fault message gets to the signal: the str
object – i.e. a string literal, eastward.g. something like "hello globe"
does not have a read
aspect.
Revisiting the erroneous code:
>>> filename = "example.txt" >>> filename . read ()
If filename
points to "case.txt", and so filename
is simply a str
object.
In other words, a file proper name is not a file object. Here's a clearer instance of errneous code:
>>> "example.txt" . read ()
And to beat out the betoken about the head:
>>> "hullo world this is just a string" . read ()
Why is this such a common mistake? Because in 99% of our typical interactions with files, we see a filename on our Desktop graphical interface and nosotros double-click that filename to open up it. The graphical interface obfuscates the process – and for adept reason. Who cares what's happening as long equally my file opens when I double-click it!
Unfortunately, we take to care when trying to read a file programmatically. Opening a file is a discrete performance from reading it.
- You lot open up a file past passing its filename – e.k.
example.txt
– into theopen()
function. Theopen up()
function returns a file object. - To actually read the contents of a file, you telephone call that file object's read() method.
Again, here's the code, in a slightly more than verbose way:
>>> myfilename = "example.txt" >>> myfile = open ( myfilename ) >>> mystuff = myfile . read () >>> # do something to mystuff, like print it, or any >>> myfile . shut ()
The file object too has a close()
method, which formally cleans upwards subsequently the opened file and allows other programs to safely access it. Again, that's a low-level detail that yous never recollect of in day-to-solar day computing. In fact, it's something you lot probably will forget in the programming context, as not endmost the file won't automatically break anything (non until we offset doing much more complicated types of file operations, at least…). Typically, as soon as a script finishes, whatsoever unclosed files will automatically be closed.
However, I like closing the file explicitly – not just to be on the safe side – just information technology helps to reinforce the concept of that file object.
How to read from a file – line-by-line
One of the advantages of getting downwardly into the lower-level details of opening and reading from files is that we now accept the ability to read files line-by-line, rather than one giant chunk. Again, to read files as one behemothic chunk of content, use the read()
method:
>>> myfile = open ( "case.txt" ) >>> mystuff = myfile . read ()
It doesn't seem like such a large deal at present, merely that'due south considering example.txt
probably contains merely a few lines. But when we bargain with files that are massive – like all 3.3 million records of anybody who has donated more than than $200 to a unmarried U.S. presidential campaign commission in 2012 or everyone who has ever visited the White House – opening and reading the file all at once is noticeably slower. And information technology may even crash your figurer.
If you've wondered why spreadsheet software, such as Excel, has a limit of rows (roughly 1,000,000), it'south because most users do desire to operate on a data file, all at one time. Still, many interesting data files are just as well big for that. We'll run into those scenarios subsequently in the quarter.
For now, here's what reading line-by-line typically looks like:
myfile = open up ( "example.txt" ) for line in myfile : impress ( line ) myfile . close ()
Considering each line in a textfile has a newline character (which is represented equally \north
but is typically "invisible"), invoking the print() part will create double-spaced output, because print() adds a newline to what it outputs (i.due east. think back to your original impress("hello world")
program).
To become rid of that effect, call the strip()
method, which belongs to str
objects and removes whitespace characters from the left and correct side of a text cord:
myfile = open ( "example.txt" ) for line in myfile : print ( line . strip ()) myfile . close ()
And of course, you lot can make things loud with the good ol' upper()
office:
myfile = open up ( "example.txt" ) for line in myfile : impress ( line . strip ()) myfile . close ()
That's information technology for now. We haven't covered how to write to a file (which is a far more dangerous operation) – I salve that for a separate lesson. Just it's plenty to know that when dealing with files as a developer, we take to exist much more explicit and specific in the steps.
References and Related Readings
Opening files and writing to files
How to open files and write to files and avoid catastrophic mistakes when writing to files.
Python Input and Output Tutorial
There are several ways to present the output of a plan; data can exist printed in a human-readable form, or written to a file for future apply. This chapter volition discuss some of the possibilities.
Source: http://www.compciv.org/guides/python/fileio/open-and-read-text-files/
0 Response to "Read the Same File Again in Python"
Post a Comment