1/93
Looks like no tags are added yet.
What are the two main operations for file handling?
Opening and closing files.
What command is used to read from the console?
The input command.
What is the purpose of file extensions?
They describe the type of file and hint at how the data is organized.
What is the basic format for opening a file?
What does the 'open' command do?
It designates a particular file and makes it ready to work with.
What is a file identifier?
A variable used to refer to a specific file.
What should you include when specifying a file name?
The file extension.
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.
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.
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).
What happens when you close a file?
You finish working with the file and free up system resources.
Can you rename a file with a different extension without changing its content?
Yes, renaming a file does not change its actual content.
What is the interactive window used for input and output called?
The terminal.
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.
What is the first step in working with a file?
You must open the file.
What do you need to do after working with a file?
You need to close the file.
What is the role of the assignment operator in the open command?
It assigns the opened file to the file identifier variable.
What type of data can be stored in files?
Files can store a variety of data types, such as text, images, and audio.
What is a common file extension for text files?
.txt
What is a common file extension for spreadsheet files?
.xlsx
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.
What does the 'r' designator signify when opening a file?
It indicates that the file will be opened for reading.
What does the 'w' designator signify when opening a file?
It indicates that the file will be opened for writing.
What does the 'a' designator signify when opening a file?
It indicates that the file will be opened for appending data.
What is the purpose of the 'rb', 'wb', and 'ab' designators?
They are used for reading, writing, or appending binary data.
What is the default mode if no designator is specified when opening a file?
'r' (read mode) is assumed.
How do you assign a file opened for reading to a variable?
myfile = open('Measurements.dat', 'r')
How do you close a file in Python?
Use the format
What is the benefit of using 'with' when opening a file?
It automatically closes the file after the indented block of code is executed.
What is the command to write a string to a file?
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.
What must you do before writing numbers to a file?
Convert them to strings.
How do you include a newline character when writing to a file?
Explicitly add '\n' in the string.
What happens if you write to a file that is opened in write mode?
It overwrites the existing content of the file.
What is the effect of writing 'myfile.write('Hello')'?
It writes 'Hello' to the file without a newline.
What does the command 'myfile.close()' do?
It closes the file and ensures it is left in a valid condition.
What is the syntax to open a file for both reading and writing in binary mode?
df = open('data', 'rb+')
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.
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.
What is the primary function of the 'write' command?
To write a string to a file.
What will happen if you do not include a newline character when writing multiple lines?
The lines will be written continuously without line breaks.
How do you write a string followed by a number to a file?
Concatenate the string with the string version of the number.
What is the result of the command 'outfile.write('Hello World!\n')'?
It writes 'Hello World!' followed by a newline to the file.
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.
What is a potential drawback of using separate open/close commands?
They may lead to issues if an error occurs before closing the file.
What is the format for writing a string to a file in Python?
What should you do after finishing all operations on a file?
Always close the file to ensure data integrity.
What command is used to open a file for writing in Python?
outfile = open('MyOutput.txt', 'w')
What does the 'write' method do in file handling?
It writes a string to the file.
What happens if you open a file for writing that already exists?
It will overwrite the existing file.
How do you ensure that a file is properly closed after writing?
Use outfile.close()
What is the purpose of the 'with' statement when opening a file?
It ensures that the file is properly closed after its suite finishes.
What does the 'readline' method do?
It reads one line from the file until a newline character is encountered.
How can you read all lines from a file in a loop?
Use 'for line in file:' to iterate through each line.
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.
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()
How do you suppress extra new lines when printing lines from a file?
Use print(next_line, end='')
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.
What is the command to open a file for reading in Python?
infile = open('filename.txt', 'r')
What happens if you try to read from a file that is not opened?
You will encounter an error.
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')
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.
What is the effect of using a double backslash in file paths?
It is used to escape the backslash character in strings.
What is the command to read an entire file into a single string?
string_variable = fileID.read()
What is the purpose of the 'close' method in file handling?
It closes the file and frees up system resources.
What does 'myfile.close()' do?
It closes the file object 'myfile'.
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.
How can you read the first line of a file?
Use next_line = myfile.readline()
What is the syntax for reading a file using a for loop?
for next_line in myfile: # process next_line
What does the 'w' mode do when opening a file?
It opens the file for writing, truncating the file first.
What is the result of not using 'end' in print when reading lines?
It will print an extra newline between each line.
What does 'myfile.readline()' return if it reaches the end of the file?
It returns an empty string.
What is a common mistake when writing to files?
Forgetting to close the file after writing.
What does the method =
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.
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.
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].
What does the .strip() method do?
It removes leading and trailing whitespace from a string.
How do you concatenate a list of strings using a separator in Python?
Use the .join() method: ).
What is an example of chaining string methods in Python?
mystr.strip().split(',') converts a string to a list after stripping whitespace.
What is the output of the code: elems = '1,2,3,4'.split(',')?
The output is the list ['1', '2', '3', '4'].
What does the dot notation signify in Python, such as in myfile.close()?
It indicates that the 'close()' operation belongs to the variable 'myfile'.
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.
What is the first step in the pseudocode for processing a CSV file?
Open the file using 'with open(filename) as myfile:'.
What does the command 'myfile.readlines()' return?
It returns a list of all lines in the file.
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)).
What is the purpose of the 'for' loop in the example pseudocode?
To iterate through each line of the file after the header.
How do you create an empty dictionary in Python?
Use the syntax: my_dict = {}.
How do you extract a student's name from a CSV line?
Combine first and last names: name = line[1] + ' ' + line[0].
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.
What does the command 'print(f"Day: {day} Month: {month} Year: {year}")' do?
It prints the day, month, and year in a formatted string.
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.
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.
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.
What is a good practice before converting pseudocode to Python code?
Practice writing out each detailed step in pseudocode.