Chapter 11: File I/O and String Processing

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/93

encourage image

There's no tags or description

Looks like no tags are added yet.

94 Terms

1
New cards

What are the two main operations for file handling?

Opening and closing files.

2
New cards

What command is used to read from the console?

The input command.

3
New cards

What is the purpose of file extensions?

They describe the type of file and hint at how the data is organized.

4
New cards

What is the basic format for opening a file?

= open("", "")
5
New cards

What does the 'open' command do?

It designates a particular file and makes it ready to work with.

6
New cards

What is a file identifier?

A variable used to refer to a specific file.

7
New cards

What should you include when specifying a file name?

The file extension.

8
New cards

What are the characteristics of offline memory compared to main memory?

Offline memory is slower to access, can store more data, and is more long-lasting.

9
New cards

What is the difference between main memory and secondary memory?

Main memory is faster to access but less permanent, while secondary memory (files) is slower but more permanent.

10
New cards

What is the purpose of the designator in the open command?

It specifies how the file will be used (e.g., read or write mode).

11
New cards

What happens when you close a file?

You finish working with the file and free up system resources.

12
New cards

Can you rename a file with a different extension without changing its content?

Yes, renaming a file does not change its actual content.

13
New cards

What is the interactive window used for input and output called?

The terminal.

14
New cards

What is the relationship between file names and their extensions?

The extension is part of the file name and indicates the file type, but it does not guarantee the file's content.

15
New cards

What is the first step in working with a file?

You must open the file.

16
New cards

What do you need to do after working with a file?

You need to close the file.

17
New cards

What is the role of the assignment operator in the open command?

It assigns the opened file to the file identifier variable.

18
New cards

What type of data can be stored in files?

Files can store a variety of data types, such as text, images, and audio.

19
New cards

What is a common file extension for text files?

.txt

20
New cards

What is a common file extension for spreadsheet files?

.xlsx

21
New cards

What does the term 'secondary storage' refer to?

It refers to storage devices that hold data outside of the main memory, such as hard drives or SSDs.

22
New cards

What does the 'r' designator signify when opening a file?

It indicates that the file will be opened for reading.

23
New cards

What does the 'w' designator signify when opening a file?

It indicates that the file will be opened for writing.

24
New cards

What does the 'a' designator signify when opening a file?

It indicates that the file will be opened for appending data.

25
New cards

What is the purpose of the 'rb', 'wb', and 'ab' designators?

They are used for reading, writing, or appending binary data.

26
New cards

What is the default mode if no designator is specified when opening a file?

'r' (read mode) is assumed.

27
New cards

How do you assign a file opened for reading to a variable?

myfile = open('Measurements.dat', 'r')

28
New cards

How do you close a file in Python?

Use the format .close()

29
New cards

What is the benefit of using 'with' when opening a file?

It automatically closes the file after the indented block of code is executed.

30
New cards

What is the command to write a string to a file?

.write()
31
New cards

What is a key difference between the write command and the print statement?

write only outputs a single string and does not add spaces or newlines automatically.

32
New cards

What must you do before writing numbers to a file?

Convert them to strings.

33
New cards

How do you include a newline character when writing to a file?

Explicitly add '\n' in the string.

34
New cards

What happens if you write to a file that is opened in write mode?

It overwrites the existing content of the file.

35
New cards

What is the effect of writing 'myfile.write('Hello')'?

It writes 'Hello' to the file without a newline.

36
New cards

What does the command 'myfile.close()' do?

It closes the file and ensures it is left in a valid condition.

37
New cards

What is the syntax to open a file for both reading and writing in binary mode?

df = open('data', 'rb+')

38
New cards

What should you do if you want to write data to a .pdf file?

Write the data in the exact binary format expected for an Adobe Acrobat file.

39
New cards

What is the main advantage of using separate open and close commands?

They are better for managing multiple files or when files are open for a long time.

40
New cards

What is the primary function of the 'write' command?

To write a string to a file.

41
New cards

What will happen if you do not include a newline character when writing multiple lines?

The lines will be written continuously without line breaks.

42
New cards

How do you write a string followed by a number to a file?

Concatenate the string with the string version of the number.

43
New cards

What is the result of the command 'outfile.write('Hello World!\n')'?

It writes 'Hello World!' followed by a newline to the file.

44
New cards

What is the effect of using the 'with' statement for file operations?

It ensures that the file is properly closed even if an error occurs.

45
New cards

What is a potential drawback of using separate open/close commands?

They may lead to issues if an error occurs before closing the file.

46
New cards

What is the format for writing a string to a file in Python?

.write('your string here')
47
New cards

What should you do after finishing all operations on a file?

Always close the file to ensure data integrity.

48
New cards

What command is used to open a file for writing in Python?

outfile = open('MyOutput.txt', 'w')

49
New cards

What does the 'write' method do in file handling?

It writes a string to the file.

50
New cards

What happens if you open a file for writing that already exists?

It will overwrite the existing file.

51
New cards

How do you ensure that a file is properly closed after writing?

Use outfile.close()

52
New cards

What is the purpose of the 'with' statement when opening a file?

It ensures that the file is properly closed after its suite finishes.

53
New cards

What does the 'readline' method do?

It reads one line from the file until a newline character is encountered.

54
New cards

How can you read all lines from a file in a loop?

Use 'for line in file:' to iterate through each line.

55
New cards

What is the difference between 'read' and 'readline'?

'read' reads the entire file as a single string, while 'readline' reads one line at a time.

56
New cards

What is the syntax to read multiple lines using a while loop?

next_line = myfile.readline(); while next_line != '': # process next_line; next_line = myfile.readline()

57
New cards

How do you suppress extra new lines when printing lines from a file?

Use print(next_line, end='')

58
New cards

What is the output of 'outfile.write(str(21))' if no newline is added?

It will write '21' on the same line as the previous content.

59
New cards

What is the command to open a file for reading in Python?

infile = open('filename.txt', 'r')

60
New cards

What happens if you try to read from a file that is not opened?

You will encounter an error.

61
New cards

What is the correct way to open a file in a different directory?

Specify the directory in the file name, e.g., open('data/data.txt', 'r')

62
New cards

What does the 'end' parameter in the print function do?

It specifies what to print at the end of the output, default is a newline.

63
New cards

What is the effect of using a double backslash in file paths?

It is used to escape the backslash character in strings.

64
New cards

What is the command to read an entire file into a single string?

string_variable = fileID.read()

65
New cards

What is the purpose of the 'close' method in file handling?

It closes the file and frees up system resources.

66
New cards

What does 'myfile.close()' do?

It closes the file object 'myfile'.

67
New cards

What is the output of 'outfile.write("Testing the write command.\n")'?

It writes 'Testing the write command.' followed by a newline to the file.

68
New cards

How can you read the first line of a file?

Use next_line = myfile.readline()

69
New cards

What is the syntax for reading a file using a for loop?

for next_line in myfile: # process next_line

70
New cards

What does the 'w' mode do when opening a file?

It opens the file for writing, truncating the file first.

71
New cards

What is the result of not using 'end' in print when reading lines?

It will print an extra newline between each line.

72
New cards

What does 'myfile.readline()' return if it reaches the end of the file?

It returns an empty string.

73
New cards

What is a common mistake when writing to files?

Forgetting to close the file after writing.

74
New cards

What does the method = .readlines() do?

It converts all the lines in the file into a list of strings, where each element is a string of one line of the file.

75
New cards

What is the purpose of the .split() method in string processing?

The .split() method converts a string into a list of strings based on a specified separator.

76
New cards

How can you split a date string formatted as month/day/year into separate variables?

Use the split method: parts = date.split('/'); month = parts[0]; day = parts[1]; year = parts[2].

77
New cards

What does the .strip() method do?

It removes leading and trailing whitespace from a string.

78
New cards

How do you concatenate a list of strings using a separator in Python?

Use the .join() method: .join().

79
New cards

What is an example of chaining string methods in Python?

mystr.strip().split(',') converts a string to a list after stripping whitespace.

80
New cards

What is the output of the code: elems = '1,2,3,4'.split(',')?

The output is the list ['1', '2', '3', '4'].

81
New cards

What does the dot notation signify in Python, such as in myfile.close()?

It indicates that the 'close()' operation belongs to the variable 'myfile'.

82
New cards

What is a common tip for dealing with files in Python?

Opening and closing files is easy, but processing strings read from files can be complex.

83
New cards

What is the first step in the pseudocode for processing a CSV file?

Open the file using 'with open(filename) as myfile:'.

84
New cards

What does the command 'myfile.readlines()' return?

It returns a list of all lines in the file.

85
New cards

How do you skip the header line when processing a CSV file?

Start your loop from index 1: for i in range(1, len(lines)).

86
New cards

What is the purpose of the 'for' loop in the example pseudocode?

To iterate through each line of the file after the header.

87
New cards

How do you create an empty dictionary in Python?

Use the syntax: my_dict = {}.

88
New cards

How do you extract a student's name from a CSV line?

Combine first and last names: name = line[1] + ' ' + line[0].

89
New cards

What is the output of the following code: print(' 1,2,3,4,5 '.strip())?

The output is '1,2,3,4,5' without leading or trailing spaces.

90
New cards

What does the command 'print(f"Day: {day} Month: {month} Year: {year}")' do?

It prints the day, month, and year in a formatted string.

91
New cards

What is the result of using 'x = all_lines[10].split('-')'?

It splits the string at index 10 of all_lines into a list using '-' as the separator.

92
New cards

What does the command 'y = "".join(all_lines[11].strip().split("-"))' do?

It joins the elements of the list created by splitting the stripped string at index 11 with '' as the separator.

93
New cards

What is the significance of the line 'all_lines[11] = x'?

It updates the list 'all_lines' at index 11 with the new list 'x' created from splitting.

94
New cards

What is a good practice before converting pseudocode to Python code?

Practice writing out each detailed step in pseudocode.