07 Files Flashcards

1
Q

What are the differences between the OSs Unix and Windows, when it comes to directories/hierarchies?

A

Unix:
- The first directory is called “root directory”.
- The character “/” is used to separate directories in a path.

Windows:
- The first directory is your hard drive, e.g. C:
- The character Blackslash “" is used to separate directories in a path.

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

How is the current folder called?

A

working directory

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

What are the absolute and relative path of a file?

A

Absolute path:
- includes the root directory
- is independent of the current working directory

Relative path:
- starts from some given working directory

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

What does the built-in function “open” do in Python?

A

It opens a file and returns its filehandle.

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

What functions of a filehandle have we learned about?

A
  • filehandle.seek()
  • filehandle.read()
  • filehandle.write()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the function filehandle.seek() do?

A

It sets the file’s cursor at a specified position in the current file.
It uses two arguments:
- offset: This is the number of positions of the cursor to move within the file.
- whence (optional): This asks from where the offset should start counting: The start of the file (0 (default)), current position of the cursor (1) or the end of the file (2).

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

What does the function filehandle.read() do?

A

Read the content of the file.

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

What does the function filehandle.write(“text”) do?

A

Writes “text” to the file.

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

What arguments does the built-in function “open” have?

A
  • filename (str)
  • mode (str)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What values are valid for the parameter “mode” for the built-in function “open”?

A
  • “r”: Read-only (fails if the file doesn’t exist)
  • “w”: Write (creates new file if it doesn’t exist, deletes original content if file already exists)
  • “a”: Append (like “write” but doesn’t delete original content if file already exists, appends new content instead)
  • “r+”: Read and Write
  • “a+”: Read and Append
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the default cursor position for the following modes, when opening a file in Python using the built-in function “open”?

  1. “r”
  2. “w”
  3. “a”
  4. “r+”
  5. “a+”
A
  1. Beginning of file
  2. Beginning of file
  3. End of file
  4. Beginning of file
  5. End of file
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does it mean if the letter “b” is added to the mode parameter of the build-in function “open” in Python?

A

It specifies that the file to open is not a text file and that it should be opened in binary mode. It won’t be interpreted that way and it only expects and returns bytes objects (instead of string objects).

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

What can happen if you don’t close your file after opening it in Python?

A
  • Not all the data is written to the file because it’s buffered and only closing the file flushes the last of the data to the file.
  • The file may get corrupted if the program terminates before closing the file.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can a file be closed in Python?

A

with the build-in function “close”.

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

How can we assure an opened file is closed, even if the program is terminated unexpectedly?

A
  • put the close-function is the finally-part of a try-finally block
  • use a with-statement that automatically closes the file for you
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s the syntax of a with-statement to read a file?

A

with open(“filepath.txt”) as fh:
content_as_string = fh.read()

17
Q

Which of the following statements is correct?

  1. The with-statement automatically closes an open file.
  2. The with-statement needs a try-finally block to ensure any open file gets closed.
  3. The with-statement handles any exception that may occur when opening a file.
  4. The with-statement needs to be put inside a try-except block to handle any exception that may occur when opening a file.
A

1 and 4 are correct

18
Q

Thinking of different OSs, what is the most flexible way to write a file path?

A

os.path.join(“folder”, “file.txt”)

This creates a compatible path according to the used OS.

19
Q

How can we store Python objects in files?

A

using the pickle or dill modules.

20
Q

What is the pickle module?

A
  • allows to store and load Python objects in form of binary files
  • doesn’t compress the files
  • can handle many different Python objects
21
Q

What is the dill module?

A
  • has the same interface as the pickle module
  • extends the pickle module
  • can handle more types of Python objects
  • should be used instead of the pickle module
  • often used like this: import dill as pickle