1 / 6

Files in Python

Files in Python. Input techniques. Input from a file. The type of data you will get from a file is always string or a list of strings. There are two ways of reading that I call “ bulk reads” because with one statement they totally exhaust the file. There is no more to read after that!

Download Presentation

Files in Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Files in Python Input techniques

  2. Input from a file • The type of data you will get from a file is always string or a list of strings. • There are two ways of reading that I call “bulk reads” because with one statement they totally exhaust the file. There is no more to read after that! • The other two ways read a line at a time from the file • Files are objects so most of these will be methods called with the dot notation as usual

  3. read() • The read method is called like this datastr =infile.read() • What does it do? it reads in the entire file of data, into one string variable • The newlines and other whitespace in the file are stored in the string like every other character • Be aware if you are reading a LARGE file, this may take some time and a lot of RAM! • This is convenient if you do not care particularly where the newlines are in the file • BULK

  4. readlines() • The syntax: datalst = infile.readlines() • This method reads in ALL the data from the file and uses the \n as a delimiter to break the data into strings in a list • There is nothing more to read in the file after you execute one readlines call. • This is convenient if you know the data in the file is organized by lines, i.e. each line needs to be processed by itself • BULK

  5. readline() • Note that this is a different method from readlines – note the s! • syntax: datastr = infile.readline() • Semantics: it reads in the next line of data from the file, up to the next newline • Returns a string which has the data and a \n character at the end • Useful when you don’t want to read in ALL the data at one time, or when you have more data than RAM space to hold it • Usually used inside a while loop • Indicates the end of the data in the file by returning an empty string. Note that this is different from having an empty or blank line in the file – that is returned as “\n”

  6. for line in infile • This is a method unique to Python • The for loop itself reads in one line from the file connected to infile and stores it in the variable of the for loop, as a string with a \n on the end • The loop automatically stops when the file is exhausted • You have to process the line inside the loop – when the loop iterates, a new line is read in • Useful when you don’t have enough RAM to hold the whole file or when you don’t want to read all the data at one time

More Related