txt to read and write python

python read and write to the txt 

filename = " D: // practice //learning_python.txt " 
# read the entire file 
with Open (filename) AS file_obj: 
    Contents = file_obj.read ()
     Print (Contents)
 # read line by line 
with Open (filename) AS file_obj: 
    Lines = file_obj.readlines ()
     Print (Lines) 
    B = Lines
 # added to the list read out the 
a = []
 for B in Lines:
     Print (B) 
    a.append (B) 
Print ( A) 

# write files
with open (filename,"w") as obj:
    obj.write("china wan sui!")
    obj.write("china people wan sui!")
    obj.write("china dang wan sui!")
#写完了读整个文件
with open (filename)as obj:    
    i = obj.read()
    for a in i:
        print(a)

 

Guess you like

Origin www.cnblogs.com/hainabaichuan/p/11830769.html