-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileHandling.py
More file actions
48 lines (35 loc) · 989 Bytes
/
Copy pathfileHandling.py
File metadata and controls
48 lines (35 loc) · 989 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
# how to open/read an existing file
# opening file in the current directory
file = open('samaha.txt')
print(file.read())
for student in file:
print(student)
# opening file in a different directory
file2 = open('C:/Users/samaha/Documents/test.txt')
print(file2.read())
# how to create a new file
myNewFile = open('students.txt', 'w')
myNewFile.write("Samaha Students \n")
myNewFile.write("Mohamed \n")
myNewFile.write("Nasteexo \n")
myNewFile.write("Lawrence \n")
myNewFile.close()
st = open('students.txt')
print(st.read())
# how to write to a file
myNewFile = open('students.txt', 'w')
myNewFile.write("University students\n")
myNewFile.close()
st = open('students.txt')
print(st.read())
# how to append to a file
myNewFile = open('students.txt', 'a')
myNewFile.write("College students\n")
myNewFile.close()
st = open('students.txt')
print(st.read())
# how to delete as file
# To delete a file, import os
# myTest = open('myTest.txt', 'x')
os.remove('mytest.txt')