-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandling.py
More file actions
89 lines (63 loc) · 1.67 KB
/
FileHandling.py
File metadata and controls
89 lines (63 loc) · 1.67 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def main():
f= open("guru99.txt","w+")
#f=open("guru99.txt","a+")
for i in range(10):
f.write("This is line %d\r\n" % (i+1))
f.close()
#Open the file back and read the contents
f=open("guru99.txt", "r")
if f.mode == 'r':
contents =f.read()
print (contents)
# or, readlines reads the individual line into a list
fl =f.readlines()
for x in fl:
print(x)
f = open("demofile.txt", "r")
for x in f:
print(x)
f = open("demofile.txt", "r")
print(f.readline())
f.close()
f = open("demofile3.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()
#open and read the file after the appending:
f = open("demofile3.txt", "r")
print(f.read())
# Python code to illustrate split() function
with open("file.text", "r") as file:
data = file.readlines()
for line in data:
word = line.split()
print word
# To open a text file, use:
# fh = open("hello.txt", "r")
# To read a text file, use:
# fh = open("hello.txt","r")
# print fh.read()
# To read one line at a time, use:
# fh = open("hello".txt", "r")
# print fh.readline()
# To read a list of lines use:
# fh = open("hello.txt.", "r")
# print fh.readlines()
# To write to a file, use:
# fh = open("hello.txt","w")
# write("Hello World")
# fh.close()
# To write to a file, use:
# fh = open("hello.txt", "w")
# lines_of_text = ["a line of text", "another line of text", "a third line"]
# fh.writelines(lines_of_text)
# fh.close()
# To append to file, use:
# fh = open("Hello.txt", "a")
# write("Hello World again")
# fh.close()
# To close a file, use
# fh = open("hello.txt", "r")
# print fh.read()
# fh.close()
if __name__== "__main__":
main()