What value does a readline method return upon encountering end of file quizlet?

Given the list below, what are the upper and lower bounds?

somelist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

opening a file and opening for reading

to access a file you need to first open it

When you open a file, you give the name of the file, or, if the file is stored in a different directory, the file name preceded by the directory path. You also specify whether the file is to be opened for reading or writing.

When opening a file for reading, the file must exist or an exception occurs.

To open a file, supply the name of the file stored on disk and the mode in which the file is to be opened.

file object returned by the open function must be saved in a variable.

infile = open("input.txt", "r")

opening a file for writing

you provide the name of the file as the first argument to the open function and the string "w" as the second argument:

outfile = open("output.txt", "w")
If the output file already exists, it is emptied before the new data is written into it. If the file does not exist, an empty file is created. When you are done processing a file, be sure to close the file using the close method:

infile.close()
outfile.close()

If your program exits without closing a file that was opened for writing, some of the output may not be written to the disk file.

reading from a file

readline method used to read a line of text
line = infile.readline()

file is opened, an input marker is positioned at the beginning of the file. The readline method reads the text, starting at the current position and continuing until the end of the line is encountered.

input marker is then moved to the next line. The readline method returns the text that it read, including the newline character that denotes the end of the line

If the file contains a blank line, then readline returns a string containing only the newline character "\n".

flying
circus
The first call to readline returns the string "flying\n". Recall that \n denotes the newline character that indicates the end of the line. If you call readline a second time, it returns the string "circus\n". Calling readline again yields the empty string "" because you have reached the end of the file.

reading multiple lines of text from a file

line = infile.readline()
while line != "" :
Process the line.
line = infile.readline()
The sentinel value is an empty string, which is returned by the readline method after the end of file has been reached.

readline method can return only strings. If the file contains numerical data, the strings must be converted to the numerical value using the int or float function:

value = float(line)

writing to a text file

Write to a file using the write method or the print function.

outfile.write("Hello, World!\n")

When writing text to an output file, however, you must explicitly write the newline character to start a new line because it does not add a new line like print statements do

write method takes a single string as an argument and writes the string immediately. That string is appended to the end of the file

ways to write to the file
outfile.write("Number of entries: %d\nTotal: %8.2f\n" % (count, total))

print("Hello, World!", file=outfile)

print("Total: ", end="", file=outfile)

iterating over lines of a file

You can iterate over a file object to read the lines of text in the file.
Python can treat an input file as though it were a container of strings in which each line is an individual string. so you can use a for loop to iterate over the strings

for line in infile :
print(line)

the difference between a file and container is that once a file has been read you cant go back and iterate through it again without closing and reopening the file

rstrip used to remove extra white space

each input line ends with a newline (\n) character.

have an input file that contains a collection of words, stored one per line

spam
and
eggs
When the lines of input are printed to the terminal, they will be displayed with a blank line between each word:

spam

and

eggs

a print function prints its argument to the terminal and then starts a new line by printing a newline character. Because each line ends with a newline character, the second newline creates a blank line in the output

to avoid the double new line - rstrip method to remove the newline character from a line of text. This creates a new string in which all white space (blanks, tabs, and newlines) at the end of the string has been removed.

line = line.rstrip()

can also use rstrip to remove ccertain characters - line = line.rstrip(".?")

character stripping methods

s.lstrip()
s.lstrip(chars)
A new version of s in which white space (blanks, tabs, and newlines) is removed from the left (the front) of s. If provided, characters in the string chars are removed instead of white space.

s.rstrip()
s.rstrip(chars)
Same as lstrip except characters are removed from the right (the end) of s.

s.strip()
s.strip(chars)
Similar to lstrip and rstrip, except characters are removed from the front and end of s.

split

Mary had a little lamb,
whose fleece was white as snow.
that we would like to print to the terminal, one word per line

Mary
had
a
little
. . .

no method for reading a word from a file, you must first read a line and then split it into individual words. This can be done using the split method:

wordList = line.split()
The split method returns the list of substrings that results from splitting the string at each blank space. For example, if line contains the string

string splitting methods

s.split()
s.split(sep)
s.split(sep, maxsplit)
Returns a list of words from string s. If the string sep is provided, it is used as the delimiter; otherwise, any white space character is used. If sep contains more than one character, then each character is treated as a separator. If maxsplit is provided, then only that number of splits will be made, resulting in at most maxsplit + 1 words.

s.rsplit(sep, maxsplit)
Same as split except the splits are made starting from the end of the string instead of from the front.

s.splitlines()
Returns a list containing the individual lines of a string split using the newline character \n as the delimiter.

string splitting examples

string = "a b c"
string.split(" ")
"a" "b" "" "c"
The string is split using the blank space as the delimiter. With an explicit argument, the consecutive blank spaces are treated as separate delimiters.

tring = "a:bc:d"
string.split(":", 1)
"a" "bc:d"
The string is split into 2 parts starting from the front. The split is made at the first colon.

string = "a:bc:d"
string.rsplit(":", 1)
"a:bc" "d"
The string is split into 2 parts starting from the end. The split is made at the last colon.

string = "a b c"
string.split()
"a" "b" "c"
The string is split using the blank space as the delimiter. Consecutive blank spaces are treated as one space.

string = "a,bc,d"
string.split(",")
"a" "bc" "d"
The string is split at each comma.

reading characters

instead of reading a whole line you can read characters using the read method

takes a single argument that specifies the number of characters to read. The method returns a string containing the characters. When supplied with an argument of 1,

char = inputFile.read(1)
the read method returns a string consisting of the next character in the file.if the end of the file is reached, it returns an empty string "". The following loop processes the contents of a file, one character at a time:

char = inputFile.read(1)
while char != "" :
Process character.
char = inputFile.read(1)

read method will read and return the newline characters that terminate the individual lines as they are encountered.

file operations

f = open(filename, mode)
Opens the file specified by the string filename. The mode parameter indicates whether the file is opened for reading ("r") or writing ("w"). A file object is returned.

f.close()
Closes a previously opened file. Once closed, the file cannot be used until it has been reopened.

string = f.readline()
Reads the next line of text from an input file and returns it as a string. An empty string "" is returned when the end of file is reached.

string = f.read(num)
string = f.read()
Reads the next num characters from the input file and returns them as a string. An empty string is returned when all characters have been read from the file. If no argument is supplied, the entire contents of the file is read and returned in a single string.

f.write(string)
Writes the string to a file opened for writing.

ways to read a file

inputFile.read() returns a string with all characters in the file

readlines method reads the entire contents of a text file into a list:
inputFile = open("sample.txt", "r")
listOfLines = inputFile.readlines()
inputFile.close()

Command line Arguments

you can run a program by hitting the run button or by typing the name of the program at the prompt in a terminal window - invoking the program from the command line

additional strings are called command line arguments

example:

python program.py -v input.dat

then the program receives two command line arguments: the strings "-v" and "input.dat". It is entirely up to the program what to do with these strings. It is customary to interpret strings starting with a hyphen (-) as program options.

argv

Programs that start from the command line receive the command line arguments in the argv list defined in the sys module.

program receives its command line arguments in the argv list defined in the sys module. In our example, the argv list has a length of 3 and contains the strings

example: python program.py -v input.dat

argv[0]: "program.py"
argv[1]: "-v"
argv[2]: "input.dat"

The first element (argv[0]) contains the name of the program, while the remaining elements contain the command-line arguments in the order they were specified.

What value does the readline () method return upon encountering end of file quizlet?

The readline method returns an empty string ("") when it has attempted to read beyond the end of a file.

What is returned when the readline method reaches the end of the file?

The readLine() method returns null when it has reached the end of a file.

When using the readline method what data type is returned?

The readline method reads one line from the file and returns it as a string.

What is the character at the end of a string returned from the readline method?

The strings returned by readlines or readline will contain the newline character at the end.