Practice: Opening Files

« Read the chapter

Q1. Create and append

BeginnerCoding

Create notes.txt with the line 'one', append the line 'two', then print the file text.

Goal: Use modes

Q2. Mode of a file

BeginnerCoding

Open m.txt for writing and print the file object's mode and name attributes.

Goal: Use attributes

Q3. Exclusive create

IntermediateCoding

Create e.txt with mode x. Try again in a try block and print 'exists' on FileExistsError.

Goal: Use x mode

Q4. Closed state

BeginnerCoding

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

IntermediateCoding

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

IntermediateCoding

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

AdvancedCoding

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

AdvancedCoding

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

AdvancedCoding

Write 'abcdef' to p.txt, open for reading, read 3 characters and print tell().

Goal: Use positions

Q10. Mode meaning

BeginnerOutput

What does this print?

Code
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

BeginnerOutput

What does this print?

Code
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

IntermediateOutput

What does this print?

Code
with open('k.txt', 'w') as f:
    f.write('x')
try:
    f.write('y')
except ValueError:
    print('closed')

Goal: Use with

Q13. Default mode

IntermediateOutput

What does this print?

Code
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

IntermediateOutput

What does this print?

Code
print(len('é'.encode('utf-8')))
print(len('é'.encode('latin-1')))

Goal: Use encoding

Q15. Exception types

AdvancedOutput

What does this print?

Code
print(issubclass(FileNotFoundError, OSError))
print(issubclass(FileExistsError, OSError))
print(issubclass(UnicodeDecodeError, OSError))

Goal: Know errors

Q16. Seek and read

AdvancedOutput

What does this print?

Code
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

BeginnerDebugging

The program should keep the old line and add a new one, but the old line is lost. Fix it.

Code
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

IntermediateDebugging

The program fails with ValueError. Fix it by reading inside the with block.

Code
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

IntermediateDebugging

The program should print 'missing' for a file that does not exist but crashes. Fix it.

Code
try:
    open('no_such_file_q.txt')
except PermissionError:
    print('missing')

Goal: Know errors

Q20. w versus x

BeginnerConceptual

Explain the difference between modes w and x.

Goal: Use modes