1.
Question 1
A student wants to write into a file called myfile, without deleting its existing content. Which one of the following functions should he or she use?
- f = open('myfile', '+')
- f = open('myfile', 'rw')
- f = open('myfile', 'a')
- f = open('myfile', 'r')
2.
Question 2
Which of the following statements are true?
A) When you open a file for reading, if the file does not exist, an error occurs.
B) When you open a file for writing, if the file does not exist, a new file is created.
C) When you open a file for reading, if the file does not exist, the program will open an empty file.
D) When you open a file for writing, if the file exists, the existing file is overwritten with the new file.
E) When you open a file for writing, if the file does not exist, an error occurs.
- B, C, and D only
- A and B only
- A, B, and D only
- All of them are correct
Question 3
Examine the following three functions that take as argument a file name and return the extension of that file. For instance, if the file name is 'myfile.tar.gz' the returned value of the function should be 'gz'. If the file name has no extention, i.e. when the file name is just 'myfile', the function should return an empty string.
- def get_extension1(filename):
- return(filename.split(".")[-1])
- def get_extension2(filename):
- import os.path
- return(os.path.splitext(filename)[1]
- def get_extension3(filename):
- return filename[filename.rfind('.'):][1:]
Which of the these functions are doing exactly what they are supposed to do according to the description above?
All of them.
get_extension3 only
get_extension2 and get_extension3
get_extension1 and get_extension3
Question 4
A student is running a Python program like this:
>python mergefasta.py myfile1.fa myfile2.fa
In the mergefasta.py program the following lines are present:
import sys
tocheck=sys.argv[1]
What is the value of the variable tocheck?
myfile2.fa
mergefasta.py
'myfile1.fa'
'myfile2.fa'
Question 5
A student launches the Python interpreter from his home directory. His home directory contains another directory called 'mydir', and 'mydir' contains two files called 'foo' and 'bar'. The home directory does not contain any files, only other directories. What will happen when he writes the following code at the Python prompt:>>> import os
>>> filenames = os.listdir('mydir')
>>> f= open(filenames[0])
1 / 1 point
A variable f representing a file object will be created, and the first file in the directory 'mydir' will be opened.
An error will be produced stating that the file to be opened does not exist.
A variable f representing a file object will be created, and the first file in the directory 'mydir' will be opened for reading in text mode.
An error will be produced stating that filename is not subscriptable.
Solutions week 4 quiz
Here are the answers to your questions:
- The correct function to use when a student wants to write into a file called ‘myfile’, without deleting its existing content is f = open('myfile', 'a'). This opens the file for appending, which means new data will be added to the end of the file without deleting its existing content.\
- The true statements are:
B) When you open a file for writing, if the file does not exist, a new file is created.
D) When you open a file for writing, if the file exists, the existing file is overwritten with the new file.
So, the correct option is A, B, and D only.
- The functions get_extension1 and get_extension3 are doing exactly what they are supposed to do according to the description above. The function get_extension2 returns the extension with a dot (e.g., ‘.gz’), which is not required according to the problem statement.
- The value of the variable tocheck will be 'myfile1.fa'. In Python, sys.argv is a list in Python, which contains the command-line arguments passed to the script. With the help of sys.argv, the Python script can be made to act like a command line program. sys.argv[0] is the name of the script itself, sys.argv[1] will be 'myfile1.fa' and so on.
- An error will be produced stating that the file to be opened does not exist. This is because os.listdir('mydir') will return a list of filenames in the directory ‘mydir’, but when trying to open the file with open(filenames[0]), Python will look for the file in the current working directory (which is the home directory), not in ‘mydir’. To open the file correctly, the code should be f = open('mydir/' + filenames[0]).
Comments
Post a Comment