-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.py
31 lines (23 loc) · 882 Bytes
/
files.py
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
28
29
30
31
import os
def main():
os.chdir("/workspace")
# Open a file for writing and create it if it doesn't exist
f = open("textfile.txt","w+")
# Open the file for appending text to the end
# f = open("textfile.txt","a+")
# write some lines of data to the file
for i in range(10):
f.write("This is line %d\r\n" % (i+1))
# close the file when done
f.close()
# Open the file back up and read the contents
f = open("textfile.txt","r")
if f.mode == 'r': # check to make sure that the file was opened
# use the read() function to read the entire file
# contents = f.read()
# print (contents)
fl = f.readlines() # readlines reads the individual lines into a list
for x in fl:
print (x)
if __name__ == "__main__":
main()