Chapter 8 Flashcards

1
Q

What is a more convenient function when we cannot use input() function to read large amounts of data?

A

We prepare the needed information as an external file and get our program to read the data in that external file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the first step in making your program read an external file?

A

We create a .txt file with the text which we want our program to read and save it on desktop or a preferred location.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s the second step?

A

We write the below script and save it as .py file in the same folder as .txt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the first line in the program do?

f=open(‘myfile.txt’,’r’)

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

Then the first argument would have been

‘C:\PythonFiles\myfile.txt’ (with double backslash \).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is ‘w’ mode?

A

for writing only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is ‘a’ mode?

A

for appending

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is ‘r+’ mode?

A

for both reading + writing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the next statement do?

firstline = f.readline()

A

It reads the first line in the .txt file and assigns it to the variable firstline

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Each time the readline() function is called, it _______. In our program, readline() was called twice. Hence the______ will be read.

A

reads a new line from the file. first two lines

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How would you tweak the code if you do not want the extra line between each line of the text?

A

print(firstline,end=’’)

its two single quotes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why is it important to close the file?

A

to free up system resources

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write an example script for using the For loop for reading the text from the .txt file

A

Find the example attached with results

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What mode will you use to write to a text file?

A

‘a’ append mode

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can we use the ‘w’ mode ?

A

we can. However, it will erase all the previous content in the file if it already exists

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Write a script for adding the below to myfile.txt
This sentence will be appended
Python is Fun

A

Each time the attached script is run, it will add
This sentence will be appended.
Python is Fun!
to myfile.txt

17
Q

How does the usage of ‘\n’ helps?

A

It adds the sentences on a new line

18
Q

Why do we want to read a file by buffer size?

A

So that our program does not use too much memory resources

19
Q

How can we open and read text files by buffer size?

A

By using the read() function.

This function allows us to use the buffer size we want

20
Q

Write a script for opening and reading text files by buffer size

A
21
Q

What do the first two lines of the code listed below do?

inputFile.txt and outputFile.txt

A

It opens two files for reading and writing respectively

22
Q

What does the statement

msg=inputFile.read(10) do?

A

The value 10 in the parentheses tells the read function to only read 10 bytes at a time

23
Q

What does the while len(msg) do?

A

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.

24
Q

Within the while loop, the statement outputFile.write(msg) _________

A

writes the message to the output file

25
Q

After writing the message, the statement msg = inputFile.read(10)?

A

reads the next 10 bytes and keeps doing it until the file is read

26
Q

To prove that only 10 bytes is read at a time, you can change the line outputFile.write(msg) in the program to ___________?

A

outputFile.write(msg+’\n’)

27
Q

What are binary files?

A

Files that contain non-text, such as image or video files

28
Q

What modes do we use for working with binary files?

A

‘rb’ or ‘wb’ mode

29
Q

Write a script for opening, reading and reproducing an image file

A
30
Q

What will the script for myimage 1 do?

A

reproduce an additional image on the desktop looking exactly like myimage.jpg

31
Q

The remove() function_______

A

deletes a file

32
Q

What is the syntax for removing a file

A

remove(‘myfile.txt’)

33
Q

The rename() function________

A

renames a file

34
Q

What is the syntax for rename function?

A

rename(‘oldfile.txt’,’newfile.txt’)