Write to a file Flashcards
1
Q
Write lines to a file
A
with open(exmp2, ‘w’) as writefile:
writefile. write("This is line A\n") writefile. write("This is line B\n")
2
Q
Read files
A
with open(exmp2, 'r') as testwritefile: print(testwritefile.read())
3
Q
Copy one file to another
A
with open(‘Example2.txt’,’r’) as readfile:
with open(‘Example3.txt’,’w’) as writefile:
for line in readfile:
writefile.write(line)
4
Q
r+, w+, a+
A
r+ : Reading and writing. Cannot truncate the file.
w+ : Writing and reading. Truncates the file.
a+ : Appending and Reading. Creates a new file, if none exists. You dont have to dwell on the specifics of each mode for this lab.