Files Flashcards
Open
open(file, mode=’r’, …)
file - pathname
Modes
r reading
w writing
x exclusive creation
a appending
t text (default)
b binary
+ updating
Read Only
r
handle at the start
raises error if file does not exist
Read and Write
r+
handle at the start
raises error if file does not exist
Write Only
w
handle at the start
truncates existing file
creates the file if it does not exists
Write and Read
w+
handle at the start
truncates existing file
creates the file if it does not exists
Append Only
a
handle at the end
creates the file if it does not exists
Append and Read
a+
handle at the end
creates the file if it does not exists
Reading Methods
read(size=-1, /)
readline(size=-1, /)
readlines(hint=-1, /)
Writing Methods
write(s, /)
writelines(lines, /)
Close
close()
With
with expression [as variable]: ...
Iterate over Lines
for line in file: ...