What are the differences between the OSs Unix and Windows, when it comes to directories/hierarchies?
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 is the current folder called?
working directory
What are the absolute and relative path of a file?
Absolute path:
- includes the root directory
- is independent of the current working directory
Relative path:
- starts from some given working directory
What does the built-in function “open” do in Python?
It opens a file and returns its filehandle.
What functions of a filehandle have we learned about?
What does the function filehandle.seek() do?
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).
What does the function filehandle.read() do?
Read the content of the file.
What does the function filehandle.write(“text”) do?
Writes “text” to the file.
What arguments does the built-in function “open” have?
What values are valid for the parameter “mode” for the built-in function “open”?
What is the default cursor position for the following modes, when opening a file in Python using the built-in function “open”?
What does it mean if the letter “b” is added to the mode parameter of the build-in function “open” in Python?
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).
What can happen if you don’t close your file after opening it in Python?
How can a file be closed in Python?
with the build-in function “close”.
How can we assure an opened file is closed, even if the program is terminated unexpectedly?
What’s the syntax of a with-statement to read a file?
with open(“filepath.txt”) as fh:
content_as_string = fh.read()
Which of the following statements is correct?
1 and 4 are correct
Thinking of different OSs, what is the most flexible way to write a file path?
os.path.join(“folder”, “file.txt”)
This creates a compatible path according to the used OS.
How can we store Python objects in files?
using the pickle or dill modules.
What is the pickle module?
What is the dill module?