u07_slides-files-flashcards

1
Q

What is the purpose of file suffixes/extensions?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do Unix and Windows differ in directory hierarchy notation?

A

Unix uses / character and starts with root directory (/), while Windows uses \ character and starts with drive ID (e.g., C:)

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

What is the difference between absolute and relative file paths?

A

Absolute paths include the root directory and are independent of working directory, while relative paths start from the current working directory

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

What is the basic syntax for opening a file in Python?

A

filehandle = open(filename: str

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

What does the ‘r’ mode do when opening a file?

A

Read-only mode - allows reading from file, fails if file doesn’t exist

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

What does the ‘w’ mode do when opening a file?

A

(Over)Write-only mode - writes to file, creates new file if doesn’t exist or deletes original content if file exists

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

What does the ‘a’ mode do when opening a file?

A

Append-only mode - writes to file, creates new file if doesn’t exist but keeps original content and appends new content to end

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

What does the ‘r+’ mode do when opening a file?

A

Read and write mode - allows reading and writing from beginning of file, fails if file doesn’t exist

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

What does the ‘a+’ mode do when opening a file?

A

Read and append mode - allows reading and writing to end of file, creates new file if doesn’t exist

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

What’s the difference between text mode and binary mode when opening files?

A

Text mode interprets file as string and works with string objects, while binary mode works with raw bytes objects

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

Why is it important to close files after opening them?

A

Written content is buffered and not necessarily saved until closed, files might be corrupted if program terminates before closing

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

What is the preferred way to handle file operations in Python?

A

Using the ‘with’ statement which automatically closes the file (more convenient than try-finally blocks)

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

How can you make file paths more portable across different operating systems?

A

Use relative paths and os.path.join() function instead of hard-coding directory separators

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

What is the difference between pickle and dill modules?

A

Both can serialize Python objects, but dill extends pickle’s functionality and can store more types of Python objects

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

What is the working directory?

A

The folder we are currently in when executing Python code

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

How do you read entire file content as a string in Python?

A

Using read() method with file handle, e.g., content = f.read()

17
Q

What are the default stream positions for different file modes?

A

‘r’, ‘w’, ‘r+’ start at beginning of file; ‘a’, ‘a+’ start at end of file