-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy path13_file_io.py
More file actions
27 lines (21 loc) · 941 Bytes
/
13_file_io.py
File metadata and controls
27 lines (21 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""
Python makes performing file I/O simple. Take a look
at how to read and write to files here:
https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
"""
# Open up the "foo.txt" file (which already exists) for reading
# Print all the contents of the file, then close the file
# Note: pay close attention to your current directory when trying to open "foo.txt"
# YOUR CODE HERE
with open("src/foo.txt", mode='r', encoding='utf-8') as f:
print(f.read())
# Open up a file called "bar.txt" (which doesn't exist yet) for
# writing. Write three lines of arbitrary content to that file,
# then close the file. Open up "bar.txt" and inspect it to make
# sure that it contains what you expect it to contain
# YOUR CODE HERE
with open("src/bar.txt", mode="w+") as f:
for i in range(3):
f.write("this is line %d\r\n" % (i+1))
with open("src/bar.txt", mode='r', encoding='utf-8') as f:
print(f.read())