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