Skip to content

Commit 64dda49

Browse files
committed
Python Course
1 parent c277a09 commit 64dda49

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

File.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,18 @@
3737
# Now add other text in file "mydata.txt" with appending "a" i=use in ".open()" function
3838
Addtext=open('mydata.txt','a')
3939
done= Addtext.write('file is create with this we can add as much as data we want to write in this file using ".open(file_name,"a") than you can append data.')
40-
Addtext.close()#after this our file is updated you can check in that file.
40+
Addtext.close()#after this our file is updated you can check in that file.
41+
42+
43+
# It is dificult to remember to close function every time
44+
# you can tackle this with below shown syntax:
45+
46+
with open('mydata.txt','r') as f:
47+
a=f.read()
48+
print(a)# now need to close this function
49+
#for write file with this syntax:
50+
51+
with open('mydata.txt','a') as f:
52+
a=f.write("now close function tension finish")
53+
print(a)
54+

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -541,4 +541,19 @@
541541
letFile= open("mydata.txt",'a')
542542
Details= letFile.write("Make this file with name mydata")
543543
letFile.close()
544-
```
544+
```
545+
546+
# Context for auto-close
547+
**You can both thing read and write with this syntax**
548+
1. to read file with this syntax:
549+
```
550+
with open('mydata.txt','r') as f:
551+
a=f.read()
552+
print(a)
553+
```
554+
2. to write file with this syntax:
555+
```
556+
with open('mydata.txt','a') as f:
557+
a=f.write()
558+
print(a)
559+
```

mydata.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Make this file with name mydatafile is create with this we can add as much as data we want to write in this file using ".open(file_name,"a") than you can append data.
1+
Make this file with name mydatafile is create with this we can add as much as data we want to write in this file using ".open(file_name,"a") than you can append data.now close function tension finish

0 commit comments

Comments
 (0)