Python basic reading and writing

Write (no additional writing at the end):
#Create a new txt file in the address on the previous line, w makes the file have write permission, and the colon makes the following text in the while loop body

f=open(r"E:\out.txt","w")
while(True):
r=input("pls. enter strings:===>")
if(r=="exit"):
                break
f.write(r)
f.close()

There must be close if there is open, and the indentation must be equal at the same level

If judgment statement must have ":"

This is a clear operation

Need to add exit at the end to end the process

Write (additional writing at the end):
#The previous line creates a new txt file in the address, w makes the file have the permission to add writing, and the colon makes the following text in the while loop body

open(r"E:\out.txt","a")
while(True):
     r=input("pls. enter strings:===>")
     if(r=="exit"):
                break
      f.write(r)
f.close()

 

read:

try:
   f=open(r"F:\Grade3\workroom\text\龚正讲话.txt","r")
   for text in f:    
       print(text)
   f.close()
except:
   print("指定路径不正确或文件不存在,请核实!")

Guess you like

Origin blog.csdn.net/weixin_44828960/article/details/108614303