70 likes | 180 Views
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!
E N D
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! • 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
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
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
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”
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