Thareja: Python Programming
Answers for Debugging Exercises: Chapter 7
Find the Output
1.
import os
Files = ['BTech.txt', 'BCA.csv', 'BSc.docx']
for file in Files:
print(os.path.join('C:\\Users\\Students', file))
Ans.
C:\Users\Students\BTech.txt
C:\Users\Students\BCA.csv
C:\Users\Students\BSc.docx
2.
with open("File.txt", "w") as file:
file.write("Greetings to All !!! \n Welcome to the world of
programming\n")
with open("File.txt") as file:
print(file.read())
Ans.
Greetings to All !!!
Welcome to the world of programming
3.
file=open("File.txt", "r")
file.read()
text = file.read())
print(len(text))
file.close()
Ans. 0
4.
str ="Welcome to Python Programming"
file = open("File.txt","w")
n = file.write(str)
©Oxford University Press. All rights reserved. 1
Thareja: Python Programming
print(n)
file.close()
Ans. None
5. What will be written in the file?
1. file.write("Oxford" + " University" + "Press")
Ans. Oxford University
2. file.write(str(len("Oxford University Press")))
Ans. 23
3. file.write("Clue".replace('C', 'B')
Ans. Blue
4. file.write("HELLO".lower())
Ans. hello
Find the Error
1.
with open("File.txt") as file
file.write("Hello World")
with open(File.txt) as f:
data = f.read()
print(data)
Ans. First line, File.txt should be opened in write mode and there
should be a colon as the last character
In the with block, there is no indentation
2.
filename = "File.txt"
file = open("filename", "r")
for line in file:
©Oxford University Press. All rights reserved. 2
Thareja: Python Programming
print(line, end = ' ')
Ans. In second line filename should not be enclosed in double quotes
3.
filename = "File.txt"
file = open(filename, "r")
while True:
print(file.readline())
Ans. The while loop will never end, so put a break statement as
if len(file.read())==0):
break
4.
file = open("File.txt", "a")
write("Hello World again")
Ans. It should be file.write(…) instead of only write(…)
Fill in the Blanks and Identify the Usage of the Lines
1. File = open("File.txt", "r")
The above statement ________ a text file.
Ans. opens
2. file.read()
The above statement ________ a text file.
Ans. reads
3. print file.readline()
The above statement ________ a text file.
Ans. reads one line at a time from
4. print file.readlines()
The above statement ________ a text file.
©Oxford University Press. All rights reserved. 3
Thareja: Python Programming
Ans. Reads a list of lines from
5. file.write("Welcome")
The above statement ________ a text file.
Ans. Writes to
6. file = open("File.txt", "w")
The above statement ________ a text file.
Ans. Opens for writing in
7. file.writelines(lines)
The above statement ________ a text file.
Ans. Write lines to
8. file = open("File.txt", "a")
The above statement ________ a text file.
Ans. Opens for appending
9. file.close()
The above statement ________ a text file.
Ans. closes
10. file.read(10)
The above statement ________ a text file.
Ans. reads first 10 bytes from
11. file.seek(file.tell()-10)
The above statement ________ a text file.
Ans. Sets the file pointer 10 bytes to the left of the current position
12. file = open("File.txt", "r+b")
The above statement ________
Ans. opens a text file for reading as well as writing in binary mode.
13. file.seek(-10,2)
The above statement ________
Ans. Moves the cursor to 10 bytes before the end of the file.
14. file.seek(20,1)
The above statement ________
Ans. Moves the cursor to 20 bytes after the current position of the file pointer.
15. file.seek(30,0)
The above statement ________
Ans. Moves the cursor to 30 bytes after the beginning of the file.
©Oxford University Press. All rights reserved. 4