1. Consider a file named rome.
txt, then the statement used to open a file for reading,
we use
a. infile = open("c:\rome.txt", "r")
b. infile = open("c:\\rome.txt", "r")
c. infile = open(file = "c:\rome.txt", "r")
d. infile = open(file = "c:\\rome.txt", "r")
2. Suppose there is a file named rome.txt, then the statement used to open a file for
writing, we use
a. outfile = open("c:\rome.txt", "w")
b. outfile = open("c:\\rome.txt", "w")
c. outfile = open(file = "c:\rome.txt", "w")
d. outfile = open(file = "c:\\rome.txt", "w")
3. Presume a file named rome.txt, then the statement used for appending data is
a. outfile = open("c:\rome.txt", "a")
b. outfile = open("c:\\rome.txt", "rw")
c. outfile = open(file = "c:\rome.txt", "w")
d. outfile = open(file = "c:\\rome.txt", "w")
4. Which of the following statements are true?
a. When you open a file for reading in ‘r’ mode, if the file does not exist, an error
occurs
b. When you open a file for writing in ‘w’ mode, if the file does not exist, a new
file is created
c. When you open a file for writing in ‘w’ mode, if the file exists, the existing file
is overwritten with the new file
7. Predict the output of the following code:
for i in range(5):
with open("data.txt", "w") as f:
if i > 0:
break
print(f.closed)
a. True
b. False
c. None
d. Error
8. The syntax to write to a CSV file is
a. CSV.DictWriter(filehandler)
b. CSV.reader(filehandler)
c. CSV.writer(filehandler)
d. CSV.write(filehandler)
9. Which of the following is not a valid mode to open a file
a. ab
b. r+
c. w+
d. rw
10. The readline() method returns
a. str
b. a list of lines
c. a list of single characters
d. a list of integers
11. Which of the following is not a valid attribute of the file object file_handler
a. file_handler.size
b. file_handler.name
c. file_handler.closed
d. file_handler.mode
12. Chose a keyword that is not an attribute of a file.
a. closed
b. softspace
c. rename
d. mode
13. The functionality of tell() method in Python is
a. tells you the current position within the file
b. tells you the end position within the file
c. tells you the file is opened or not
d. None of the above
14. The syntax for renaming of a file is
a. rename(current_file_name, new_file_name)
b. rename(new_file_name, current_file_name,)
c. rename(()(current_file_name, new_file_name))
d. None of the above
15. To remove a file, the syntax used is,
a. remove(file_name)
b. (new_file_name, current_file_name,)
c. remove((), file_name))
d. None of the above
16. An absolute path name begins at the
a. leaf
b. stem
c. root
d. current directory
17. The functionality of seek() function is
a. sets the file’s current position at the offset
b. sets the file’s previous position at the offset
c. sets the file’s current position within the file
d. None of the above
18. What is unpickling?
a. It is used for object de- serialization
b. It is used for object serialization
c. It is used for synchronization
d. It is used for converting an object to its string representation
19. Which of the following are basic I/O connections in the file?
a. Standard Input
b. Standard Output
c. Standard errors
d. All of the above
20. The mode that is used to refer to binary data is
a. r
b. w
c. +
d. b
21. File type is represented by its
a. file name
b. file extension
c. file identifier
d. file variable
22. The method that returns the time of last modification of the file is
a. getmtime()
b. gettime()
c. time()
d. localtime()
23. Pickling is used for?
a. object deserialization
b. object serialization
c. synchronization
d. converting string representation to object
1.Define file and explain the different types of files.
Un archivo o fichero informático es un conjunto de bits que son almacenados en un
dispositivo.Python supports two types of files – text files and binary files.
2. Explain the different file mode operations with examples.
file_handler = open("example.txt","x")
file_handler = open("moon.txt","r")
file_handler = open("C:\langur.txt","r")
file_handler = open("C:\prog\example.txt","r")
file_handler = open("C:\\fauna\\bison.txt","r")
file_handler = open("C:\\network\computer.txt","r")
file_handler = open(r"C:\network\computer.txt","r")
file_handler = open("titanic.txt","r")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'titanic.txt'
9. >>> file_handler = open("titanic.txt","w+")
10. >>> file_handler = open("titanic.txt","a+")
3. Describe with an example how to read and write to a text file.
f = open("ARCHIVO.txt", "w")
f.write("abcdefgh")
f.close()
5. Illustrate with an example how to read and write a csv file.
import csv
def main():
with open('biostats.csv', newline='') as csvfile:
csv_reader = csv.reader(csvfile)
print("Print each row in CSV file")
for each_row in csv_reader:
print(",".join(each_row))
if __name__ == "__main__":
main()
6. Describe all the methods available in the os module.