Practice: Opening Files
Q1. Create and append
Create notes.txt with the line 'one', append the line 'two', then print the file text.
Goal: Use modes
Q2. Mode of a file
Open m.txt for writing and print the file object's mode and name attributes.
Goal: Use attributes
Q3. Exclusive create
Create e.txt with mode x. Try again in a try block and print 'exists' on FileExistsError.
Goal: Use x mode
Q4. Closed state
Show that the file is closed after the with block by printing f.closed before and after the block.
Goal: Use with
Q5. Encoded size
Write 'naïve' in UTF-8 to u.txt and print the size in bytes read back in rb mode.
Goal: Use encoding
Q6. Copy with upper case
Write 'abc' to a.txt, then copy it uppercased to b.txt using one with statement for both files. Print b.txt.
Goal: Open two files
Q7. Replace the start
Write 'hello world' to r.txt, open it with r+, write 'J' at the start and print the whole text.
Goal: Use r+
Q8. Handle a folder
Try to open the current folder '.' for reading and print the name of the exception class caught as OSError.
Goal: Know errors
Q9. Tell position
Write 'abcdef' to p.txt, open for reading, read 3 characters and print tell().
Goal: Use positions
Q10. Mode meaning
What does this print?
with open('o.txt', 'w') as f:
f.write('abc')
with open('o.txt', 'a') as f:
f.write('def')
print(open('o.txt').read())Goal: Use modes
Q11. Erase on open
What does this print?
with open('z.txt', 'w') as f:
f.write('data')
open('z.txt', 'w').close()
print(len(open('z.txt').read()))Goal: Use modes
Q12. After the block
What does this print?
with open('k.txt', 'w') as f:
f.write('x')
try:
f.write('y')
except ValueError:
print('closed')Goal: Use with
Q13. Default mode
What does this print?
with open('d.txt', 'w') as f:
f.write('x')
with open('d.txt') as f:
print(f.mode)Goal: Use modes
Q14. Encoding bytes
What does this print?
print(len('é'.encode('utf-8')))
print(len('é'.encode('latin-1')))Goal: Use encoding
Q15. Exception types
What does this print?
print(issubclass(FileNotFoundError, OSError))
print(issubclass(FileExistsError, OSError))
print(issubclass(UnicodeDecodeError, OSError))Goal: Know errors
Q16. Seek and read
What does this print?
with open('s.txt', 'w') as f:
f.write('0123456789')
with open('s.txt') as f:
f.seek(4)
print(f.read(3))
print(f.tell())Goal: Use positions
Q17. Fix the mode
The program should keep the old line and add a new one, but the old line is lost. Fix it.
with open('h.txt', 'w') as f:
f.write('old\n')
with open('h.txt', 'w') as f:
f.write('new\n')
print(open('h.txt').read(), end='')Goal: Use modes
Q18. Fix the closed file
The program fails with ValueError. Fix it by reading inside the with block.
with open('i.txt', 'w') as f:
f.write('hi')
with open('i.txt') as f:
pass
print(f.read())Goal: Use with
Q19. Fix the exception
The program should print 'missing' for a file that does not exist but crashes. Fix it.
try:
open('no_such_file_q.txt')
except PermissionError:
print('missing')Goal: Know errors
Q20. w versus x
Explain the difference between modes w and x.
Goal: Use modes