-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_basic.py
More file actions
35 lines (29 loc) · 866 Bytes
/
Copy pathfile_basic.py
File metadata and controls
35 lines (29 loc) · 866 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
#Read all lines from the file and this is very fast and memory efficient
f=open('file1','r')
for x in f:
print x
f.close()
##################################
#This also read all lines from the file it is not memory efficient because it read all data from the file and put it into the memory
f=open('file1','r')
for x in f.readlines():
print x
f.close()
##################################
#list(f) read all lines from the file in a list
f=open('file1','r')
print list(f)
f.close()
##################################
#write(string) writes the content of string to the file
f=open('file1','w')
f.write('This is anusree\n')
f.write('hello world\n')
f.close()
#################################
#To write something other than string needs to be converted to string first
f=open('file1','a')
value=('The answer is: ', 42)
s=str(value)
f.write(s)
f.close()