37 lines
1.0 KiB
Markdown
37 lines
1.0 KiB
Markdown
|
|
# Chapter 8 – Reading and Writing Files
|
|||
|
|
|
|||
|
|
> Q: 1. What is a relative path relative to?
|
|||
|
|
|
|||
|
|
The current working directory.
|
|||
|
|
|
|||
|
|
> Q: 2. What does an absolute path start with?
|
|||
|
|
|
|||
|
|
The root folder. such as `/` or `C:\`
|
|||
|
|
|
|||
|
|
> Q: 3. What do the os.getcwd() and os.chdir() functions do?
|
|||
|
|
|
|||
|
|
Get current path, and change the current working directory.
|
|||
|
|
|
|||
|
|
> Q: 4. What are the . and .. folders?
|
|||
|
|
|
|||
|
|
`.` is current folder, `..` is the parent folder.
|
|||
|
|
|
|||
|
|
> Q: 5. In C:\bacon\eggs\spam.txt, which part is the dir name, and which part is the base name?
|
|||
|
|
|
|||
|
|
`C:\bacon\eggs` is the dir name, `spam.txt` is the base name.
|
|||
|
|
|
|||
|
|
> Q: 6. What are the three “mode” arguments that can be passed to the open() function?
|
|||
|
|
|
|||
|
|
`r`, `w`, `a`.
|
|||
|
|
|
|||
|
|
> Q: 7. What happens if an existing file is opened in write mode?
|
|||
|
|
|
|||
|
|
Existing file will be erased and completely overwritten.
|
|||
|
|
|
|||
|
|
> Q: 8. What is the difference between the read() and readlines() methods?
|
|||
|
|
|
|||
|
|
`read()` returns the file's entire contents as a single string. `readlines()` returns a list of each lines.
|
|||
|
|
|
|||
|
|
> Q: 9. What data structure does a shelf value resemble?
|
|||
|
|
|
|||
|
|
dictionary.
|