u07_slides-files-flashcards
What is the purpose of file suffixes/extensions?
File suffixes indicate the file type (encoding/decoding schema) to the OS and are part of the filename - they help OS choose appropriate program to open the file
How do Unix and Windows differ in directory hierarchy notation?
Unix uses / character and starts with root directory (/), while Windows uses \ character and starts with drive ID (e.g., C:)
What is the difference between absolute and relative file paths?
Absolute paths include the root directory and are independent of working directory, while relative paths start from the current working directory
What is the basic syntax for opening a file in Python?
filehandle = open(filename: str
What does the ‘r’ mode do when opening a file?
Read-only mode - allows reading from file, fails if file doesn’t exist
What does the ‘w’ mode do when opening a file?
(Over)Write-only mode - writes to file, creates new file if doesn’t exist or deletes original content if file exists
What does the ‘a’ mode do when opening a file?
Append-only mode - writes to file, creates new file if doesn’t exist but keeps original content and appends new content to end
What does the ‘r+’ mode do when opening a file?
Read and write mode - allows reading and writing from beginning of file, fails if file doesn’t exist
What does the ‘a+’ mode do when opening a file?
Read and append mode - allows reading and writing to end of file, creates new file if doesn’t exist
What’s the difference between text mode and binary mode when opening files?
Text mode interprets file as string and works with string objects, while binary mode works with raw bytes objects
Why is it important to close files after opening them?
Written content is buffered and not necessarily saved until closed, files might be corrupted if program terminates before closing
What is the preferred way to handle file operations in Python?
Using the ‘with’ statement which automatically closes the file (more convenient than try-finally blocks)
How can you make file paths more portable across different operating systems?
Use relative paths and os.path.join() function instead of hard-coding directory separators
What is the difference between pickle and dill modules?
Both can serialize Python objects, but dill extends pickle’s functionality and can store more types of Python objects
What is the working directory?
The folder we are currently in when executing Python code