Python Read Text File and Check for a String
Opening files and reading from files
How to opening files and read from files and avert abrasive mistakes when reading files
Summary
Opening files and reading their data is something we learn how to practice with a unproblematic double-click in our earliest interactions with computers. Still, at the programmatic layer, things are essentially more complicated…
The basic pattern of opening and reading files in Python
Here'south the official Python documentation on reading and writing from files. But before reading that, permit's dive into the bare minimum that I want you to know.
Allow's only become directly to a code example. Pretend yous have a file named example.txt in the current directory. If you lot don't, just create one, and then fill it with these lines and save it:
hello globe and now I say good day Hither's a short snippet of Python code to open that file and print out its contents to screen – annotation that this Python code has to exist run in the same directory that the example.txt file exists in.
myfile = open ( "example.txt" ) txt = myfile . read () print ( txt ) myfile . close () Did that seem too complicated? Here's a less verbose version:
myfile = open up ( "example.txt" ) print ( myfile . read ()) myfile . close () Here's how to read that file, line-past-line, using a for-loop:
myfile = open ( "example.txt" ) for line in myfile : impress ( line ) myfile . shut () (Notation: If you lot're getting a FileNotFoundError already – that'due south almost to be expected. Keep reading!)
Nevertheless seem also complicated? Well, there's no getting around the fact that at the programmatic layer, opening a file is distinct from reading its contents. Not simply that, nosotros also have to manually close the file.
At present let's take this stride-by-step.
How to open a file – an interactive exploration
To open a file, we simply apply the open up() method and laissez passer in, as the starting time argument, the filename:
myfile = open ( "case.txt" ) That seems easy enough, and so let's jump into some common errors.
How to mess up when opening a file
Hither is likely the most mutual error you'll get when trying to open up a file.
FileNotFoundError: [Errno 2] No such file or directory: 'SOME_FILENAME' In fact, I've seen students waste dozens of hours trying to get by this error message, because they don't stop to read information technology. So, read it: What does FileNotFoundError mean?
Try putting spaces where the capitalization occurs:
File Not Found Error You'll get this error because yous tried to open a file that simply doesn't exist. Sometimes, information technology's a elementary typo, trying to open() a file named "example.txt" but accidentally misspelling information technology every bit "exmple.txt".
But more often, it's considering you know a file exists nether a given filename, such as "example.txt" – but how does your Python lawmaking know where that file is? Is it the "example.txt" that exists in your Downloads folder? Or the one that might exist in your Documents folder? Or the thousands of other folders on your computer organisation?
That'due south a pretty complicated question. Simply the showtime step in non wasting your time is that if yous ever come across this error, end whatever else you are doing. Don't tweak your convoluted for-loop. Don't try to install a new Python library. Don't restart your figurer, then re-run the script to see if the error magically fixes itself.
The mistake FileNotFoundError occurs because you either don't know where a file actually is on your computer. Or, even if yous practise, you lot don't know how to tell your Python programme where it is. Don't attempt to fix other parts of your code that aren't related to specifying filenames or paths.
How to fix a FileNotFoundError
Here's a surefire fix: make sure the file actually exists.
Let's start from scratch past making an error. In your system shell (i.eastward. Terminal), change to your Desktop folder:
$ cd ~/Desktop Now, run ipython:
$ ipython And at present that you're in the interactive Python interpreter, try to open a filename that you know does not exist on your Desktop, and so enjoy the error bulletin:
>>> myfile = open ( "whateverdude.txt" ) --------------------------------------------------------------------------- FileNotFoundError Traceback (nearly contempo telephone call last) <ipython-input-i-4234adaa1c35> in <module>() ----> ane myfile = open up("whateverdude.txt") FileNotFoundError: [Errno 2] No such file or directory: 'whateverdude.txt' Now manually create the file on your Desktop, using Sublime Text three or whatever you lot desire. Add together some text to it, then salvage it.
this is my file Look and see for yourself that this file actually exists in your Desktop folder:
OK, now switch back to your interactive Python beat out (i.e. ipython), the 1 that you opened after changing into the Desktop binder (i.e. 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 error.
Simply 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 of import, other than to point out that myfile is most definitely not just a string literal, i.e. str.
Utilise the Tab autocomplete (i.eastward. type in myfile.) to become 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.mode myfile.seekable myfile.detach myfile.proper noun myfile.tell myfile.encoding myfile.newlines myfile.truncate myfile.errors myfile.read myfile.writable myfile.fileno myfile.readable myfile.write myfile.affluent myfile.readline myfile.writelines Well, we tin do a lot more than with files than just read() from them. But allow's focus on just reading for at present.
How to read from a file – an interactive exploration
Bold the myfile variable points to some kind of file object, this is how you read from it:
>>> mystuff = myfile . read () What's in that mystuff variable? Over again, use the type() function:
>>> blazon ( mystuff ) str It'due south simply a cord. Which ways of form that nosotros can impress it out:
>>> print ( mystuff ) this is my file Or count the number of characters:
>>> len ( mystuff ) 15 Or print it out in all-caps:
>>> print ( mystuff . upper ()) THIS IS MY FILE And that's all at that place's to reading from a file that has been opened.
Now onto the mistakes.
How to mess up when reading from a file
Here's a very, very common fault:
>>> filename = "example.txt" >>> filename . read () The error output:
AttributeError Traceback (most recent call concluding) <ipython-input-nine-441b57e838ab> in <module>() ----> 1 filename.read() AttributeError: 'str' object has no attribute 'read' Take conscientious note that this is non a FileNotFoundError. It is an AttributeError – which, admittedly, is not very clear – but read the next part:
'str' object has no attribute 'read' The error message gets to the bespeak: the str object – i.e. a string literal, e.g. something like "hello world" does not have a read attribute.
Revisiting the erroneous code:
>>> filename = "example.txt" >>> filename . read () If filename points to "example.txt", then filename is simply a str object.
In other words, a file proper name is non a file object. Here's a clearer instance of errneous code:
>>> "example.txt" . read () And to beat the point about the head:
>>> "how-do-you-do world this is just a string" . read () Why is this such a mutual error? Because in 99% of our typical interactions with files, we see a filename on our Desktop graphical interface and we double-click that filename to open it. The graphical interface obfuscates the process – and for practiced reason. Who cares what's happening as long as my file opens when I double-click it!
Unfortunately, we have to care when trying to read a file programmatically. Opening a file is a discrete operation from reading it.
- You lot open a file by passing its filename – eastward.g.
example.txt– into theopen up()function. Theopen()office returns a file object. - To actually read the contents of a file, yous telephone call that file object'southward read() method.
Again, here'due south the code, in a slightly more than verbose fashion:
>>> myfilename = "example.txt" >>> myfile = open ( myfilename ) >>> mystuff = myfile . read () >>> # do something to mystuff, like print it, or whatever >>> myfile . close () The file object also has a close() method, which formally cleans up after the opened file and allows other programs to safely access it. Again, that's a depression-level detail that you lot never think of in twenty-four hours-to-day computing. In fact, it'southward something you probably will forget in the programming context, as non endmost the file won't automatically break anything (not until nosotros start doing much more complicated types of file operations, at least…). Typically, as soon every bit a script finishes, whatever unclosed files will automatically be closed.
However, I similar closing the file explicitly – not merely to be on the safe side – but information technology helps to reinforce the concept of that file object.
How to read from a file – line-past-line
One of the advantages of getting down into the lower-level details of opening and reading from files is that we now have the power to read files line-by-line, rather than one giant chunk. Again, to read files every bit one giant clamper of content, use the read() method:
>>> myfile = open ( "example.txt" ) >>> mystuff = myfile . read () Information technology doesn't seem like such a big deal now, but that'south because example.txt probably contains just a few lines. Merely when we deal with files that are massive – similar all three.three million records of everyone who has donated more than $200 to a single U.S. presidential campaign committee in 2012 or everyone who has ever visited the White House – opening and reading the file all at one time is noticeably slower. And it may even crash your reckoner.
If you've wondered why spreadsheet software, such every bit Excel, has a limit of rows (roughly 1,000,000), information technology's because most users practice want to operate on a data file, all at once. Still, many interesting data files are simply too large for that. We'll run into those scenarios later in the quarter.
For now, here's what reading line-by-line typically looks similar:
myfile = open ( "example.txt" ) for line in myfile : impress ( line ) myfile . shut () Because each line in a textfile has a newline graphic symbol (which is represented as \n but is typically "invisible"), invoking the print() role volition create double-spaced output, because print() adds a newline to what it outputs (i.east. retrieve back to your original print("howdy world") plan).
To get rid of that upshot, call the strip() method, which belongs to str objects and removes whitespace characters from the left and correct side of a text string:
myfile = open up ( "instance.txt" ) for line in myfile : print ( line . strip ()) myfile . shut () And of course, you lot can make things loud with the expert ol' upper() function:
myfile = open up ( "instance.txt" ) for line in myfile : print ( line . strip ()) myfile . close () That'south it for at present. We haven't covered how to write to a file (which is a far more dangerous performance) – I save that for a carve up lesson. But it's plenty to know that when dealing with files as a programmer, we have to be 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
In that location are several ways to nowadays the output of a program; information can be printed in a human being-readable class, or written to a file for hereafter utilize. This chapter volition discuss some of the possibilities.
Source: http://www.compciv.org/guides/python/fileio/open-and-read-text-files/
0 Response to "Python Read Text File and Check for a String"
إرسال تعليق