Chapter 8 Flashcards
What is a more convenient function when we cannot use input() function to read large amounts of data?
We prepare the needed information as an external file and get our program to read the data in that external file.
What is the first step in making your program read an external file?
We create a .txt file with the text which we want our program to read and save it on desktop or a preferred location.
What’s the second step?
We write the below script and save it as .py file in the same folder as .txt
What does the first line in the program do?
f=open(‘myfile.txt’,’r’)
It opens the .txt file and reads it. r= read mode
open() function open the .txt file
the two arguments entail the path of the file and the mode
How would the first argument look like if the .txt and .py file weren’t saved in the same folder but a different folder in the C drive
Then the first argument would have been
‘C:\PythonFiles\myfile.txt’ (with double backslash \).
What is ‘w’ mode?
for writing only
What is ‘a’ mode?
for appending
What is ‘r+’ mode?
for both reading + writing
What does the next statement do?
firstline = f.readline()
It reads the first line in the .txt file and assigns it to the variable firstline
Each time the readline() function is called, it _______. In our program, readline() was called twice. Hence the______ will be read.
reads a new line from the file. first two lines
How would you tweak the code if you do not want the extra line between each line of the text?
print(firstline,end=’’)
its two single quotes
Why is it important to close the file?
to free up system resources
Write an example script for using the For loop for reading the text from the .txt file
Find the example attached with results
What mode will you use to write to a text file?
‘a’ append mode
Can we use the ‘w’ mode ?
we can. However, it will erase all the previous content in the file if it already exists
Write a script for adding the below to myfile.txt
This sentence will be appended
Python is Fun
Each time the attached script is run, it will add
This sentence will be appended.
Python is Fun!
to myfile.txt
How does the usage of ‘\n’ helps?
It adds the sentences on a new line
Why do we want to read a file by buffer size?
So that our program does not use too much memory resources
How can we open and read text files by buffer size?
By using the read() function.
This function allows us to use the buffer size we want
Write a script for opening and reading text files by buffer size
What do the first two lines of the code listed below do?
inputFile.txt and outputFile.txt
It opens two files for reading and writing respectively
What does the statement
msg=inputFile.read(10) do?
The value 10 in the parentheses tells the read function to only read 10 bytes at a time
What does the while len(msg) do?
It checks the length of the variable msg. This means that as long as the length is not zero, the while loop will continue to run.
Within the while loop, the statement outputFile.write(msg) _________
writes the message to the output file
After writing the message, the statement msg = inputFile.read(10)?
reads the next 10 bytes and keeps doing it until the file is read
To prove that only 10 bytes is read at a time, you can change the line outputFile.write(msg) in the program to ___________?
outputFile.write(msg+’\n’)
What are binary files?
Files that contain non-text, such as image or video files
What modes do we use for working with binary files?
‘rb’ or ‘wb’ mode
Write a script for opening, reading and reproducing an image file
What will the script for myimage 1 do?
reproduce an additional image on the desktop looking exactly like myimage.jpg
The remove() function_______
deletes a file
What is the syntax for removing a file
remove(‘myfile.txt’)
The rename() function________
renames a file
What is the syntax for rename function?
rename(‘oldfile.txt’,’newfile.txt’)