07 Files Flashcards
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?
- filehandle.seek()
- filehandle.read()
- filehandle.write()
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?
- filename (str)
- mode (str)
What values are valid for the parameter “mode” for the built-in function “open”?
- “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
What is the default cursor position for the following modes, when opening a file in Python using the built-in function “open”?
- “r”
- “w”
- “a”
- “r+”
- “a+”
- Beginning of file
- Beginning of file
- End of file
- Beginning of file
- End of file
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?
- 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 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?
- put the close-function is the finally-part of a try-finally block
- use a with-statement that automatically closes the file for you