Python learning 16: Read and Write files

Source: Internet
Author: User

A simple text editor

#-*-coding:utf-8-*-from sys import argv # import argv function from sys module, #"argv" is a shorthand for "argument variable" parameter variable. It is generally passed to the program by the system at the command line invocation. # is typically passed to the program by the system at the command line invocation. script, filename = argv # Use the argv function to unpack the contents of the argv and assign all the parameters to the left variable name print "We ' re going to erase%r."% FileName # print file name parameter Prin  T "If you don ' t want this, hit Ctrl-c (^c)." # Prompt input Ctrl + C on Exit program print "If you do want this, hit RETURN." Printprint The original file: "Printtxt = open" opens the file using the Open function and saves the contents to the TXT variable in print txt.read () # reads the contents of the TXT and prints it out, in order to read the files that are not purged Content Raw_input ("?") before it is written to other content Print "Opening the file ..." target = open (filename, ' W ') # Opens the document in write mode print "truncating. goodbye! " Target.truncate () # Clears the contents of the file with the Truncate function print "Now I ' m going to ask you for three lines." Print#line1 = Raw_input ("Line 1:") # writes three lines of content, saved to Line1,line2 and line3 variables txt1 = open (filename, ' W ') txt1.write (' This is a test . \nreally, it is. ') Txt1.close () printprint "I ' m going to write these to the file." Printtarget.write (line2) # Use the Write function to put the line1 variable target.write ("\ n") # using the Write functionWrite a newline character target.write (line2) #使用write函数把line2变量中target. Write ("\ n") # Write a line break with the Write function Target.write (line3) # Use the Write function to put the LINE3 variable in target.write ("\ n") # using the Write function to write a line break Printprint "and finally, we close it." Target.close () # close File filename1 = raw_input ("Input a filename:") Filename1_open = Open (filename1) # Open File print "Here is the New file content: "Printprint filename1_open.read () # reads the contents of the newer files print" Close opened file ... "Filename1_open.close () # Close files

Python learning 16: Reading and writing files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.