forked from codehouseindia/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile Handling program
More file actions
140 lines (135 loc) · 2.81 KB
/
File Handling program
File metadata and controls
140 lines (135 loc) · 2.81 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#https://www.facebook.com/piyush.chandana.3/posts/1282062888823657
#Subscribed by Piyush Chandana
#file handling
'''
--> filehandling is the process of creating text,sv,tsv,excel,files and writing
some content into file,delete content from the file update into file.
'''
#python provide different types of modes to access file
'''
1>write mode(w)
2>read mode(r)
3>append mode(a)
4>write+mode(w+)
5>read+mode(r+)
6>append+mode(a+)
7>write in binary(wb)
8>read binary(rb)
'''
#w mode
'''
--> In write mode file can be created if file does not exist with specified file
name otherwise all the content of the file will be removed.
--> We can write some thing into file.
'''
'''
f=open("sample.text","w")
print(f.write("This is my first line\n"))
f.write("This is my first line.")
f.write("\n")
f.write("This is my second line")
f.close()
'''
#read mode
'''
--> In this mode we can read content from file or represent content of file
by a variable name if file exist othherwise raise FileNotFoundError.
'''
'''
f = open('sample.txt','r')
print(f.read())
f.seek(0)
print(f.read(7))
print(f.read())
f.seek(21)
print(f.read())
f.close()
'''
'''
f=open("sample.txt")
data=f.readlines()
print(data)
for i in data:
print(i)
f.close()
'''
#access each word of the file
'''
f=open("sample.txt","r")
data=f.read().split()
print(data)
for i in data:
print(i)
f.close()
'''
#3>append mode
'''
--> This mode provide environment to create
a flie with specified name if file does
not exist.
--> In this mode we can write something at
the last index of the file.
'''
'''
f=open("demo.txt","a")
f.write("Hello,how are you.")
f.write("\nwow previous content is existing")
f.close()
'''
# read+mode(r+)
'''
--> In this mode,we can write into file
as well as read from file.
--> As we open file in r+ mode,cursor will be
at start index.
--> When we write into file then existing characteris relaced by new character.
'''
'''
f=open("demo.txt","r+")
#print(f.read())
f.write("this")
f.seek(13)
f.write("we")
print(f.read())
f.close()
'''
#write+mode(w+)
'''
--> all features of write mode will be inherited.
--> additional features is that,we can read content of file.
'''
#creating new file
'''
f=open("demo1.txt","w+")
f.close()
'''
#removing content of existing file
'''
with open("demo.txt","w+") as f:
f.write("ägain create first line.")
f.close()
'''
#6> append +mode
'''
-->all the features of append mode will be inherited
-->reading content of file is additional feature.
'''
with open("demo.txt","a+") as f:
f.write("\nThis is second line")
f.seek(0)
print(f.read())
f.close()
'''
f=open("demo.txt")
data=f.readlines()
f.close()
list1=[]
for i,v in enumerate(data,start=1):
if(i==3):
v=v.replace("This","That")
list1.append(v)
f=open("demo.txt","w")
for i in list1:
f.write(i)
f.close()
'''