python file read and write (additionally, cover)

(1) In lucky.txt new content (covering: Each run will rewrite the contents)

= F " lucky.txt " 

A =. 8 
with Open (F, " W " ) AS File:    # "W" represents the contents of each run cover
     for I in Range (A): 
        a file.write (STR (I) + " D " + "  " + " \ n- " ) 
    A +. 1 =

  Output:

    

 

(2) additional content lucky.txt the (additional: Before txt content is not changed, continue to add content to the content that already exists)

= f " lucky.txt " 

A = 8 
with Open (f, " A " ) AS File:   Before the # only need to "w" was changed to "a" to, on behalf of additional content
     for i in the Range (A): 
        a file.write (STR (I) + " D " + "  " + " \ n- " ) 
    A +. 1 =

  Output:

  

 

According to table mode when starting, if necessary, to change the open file: Summary.

 

Guess you like

Origin www.cnblogs.com/syw20170419/p/10972471.html