Module 7 Flashcards

1
Q

How do you open a file

A

open it: supply the name of the file stored on disk and the mode in which the file is to be opened

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

infile = open (“input.txt”, “r”)

A

opens the file for reading (indicated by the string argument “r”) and returns a file object that is associated with the name input.txt

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

all operations for accessing a file are made via what?

A

the file object

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

what are text files commonly used for?

A

to store information. The most “portable” type of data files

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

examples of text files that are created with a simple text editor

A

windows notepad, HTMML, and Python source code

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

What are some important things to keep in mind when opening a file for reading?

A
  • the file must exist (and otherwise
    be accessible) or an exception occurs
  • The file object returned by the open function must be saved in a
    variable
  • All operations for accessing a file are made via the file object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

file objects allows us to…

A

to use, access, and manipulate all the user accessible files.

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

outfile = open(“output.txt”, “w”)

A

To open 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

f 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

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

closing files

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