Write to a file Flashcards

You may prefer our related Brainscape-certified 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")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Read files

A
with open(exmp2, 'r') as testwritefile:
    print(testwritefile.read())
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly