Python Basic Notes Series 11: standard input and output, file read/write, pointers, etc.

Source: Internet
Author: User

This series of tutorials is intended for individual learning notes, and if you want to browse may require other programming language basics (such as C), why? Because I write rotten ah, only I can understand!!

  • standard input and output
    One, input
    You need to install the SUBLIMEREPL plugin at this time in sublime. Ctrl+shift+p type Install packages, and then continue typing the SUBLIMEREPL installation and then each time the compilation is run: Tools->sublimerepl->python->python-run Current file. After clicking the new page *repl*[python], as a new console, you can enter and output the interaction. To view another, Portal: Python basic Notes Series IV: Installation and configuration of tools
    1) using the Raw_input () function
    1 buff = raw_input ('pleaseinput:')  # Whatever you enter is String type 2  print  buff3print type (buff)

    2) using the input () function

    1 buff2 = input ('pleaseinput:')  # Input If the number is int type 2  print  buff23print type (BUFF2)

    Second, the output

      The simplest output method is the print statement, which you can pass with 0 or more comma-separated expressions, or use a placeholder.
    Example:
    1 num =2002print 1,3,5,'num=%d'%num

    Output:

    1 1 3 5 num=200

  • File read/write
    One, open and close files
    The first thing to do is to open a file to create the file object before you can read and write to it.

    #1. Open: Use the Open function
    #2. Close: Use the Close function: Flushes any information that has not been written in the buffer, closes the file, and then cannot read and write to the file.
    # Flush Method Flush buffer can also be called before close
    # fil = open (filename, open file mode, buffer), where the following two parameters can be omitted, the pattern has read (R), write (W), append (a), etc.
    #缓冲区 (equals 0: does not register, equals 1: One row, greater than 1: Cache size of the storage area, less than 0: system default buffer size)
    Second, read the file
    After opening a file in ' R ' mode, you can call the Read function to read all the contents of the file at once, or you can specify how many bytes to read at a time.
    Example:

    1FIL1 = open ('Yyc.txt','R',-1)#The read file must exist (currently in the current directory)2 #buff3 =fil1.read () #一次性读完3Buff4 =fil1.read (100)#Read 100 bytes4 #print buff3+ "," +buff4 #这里如果先read () and then read (100) The Buff4 is empty, because read () has finished reading the entire file, just like the pointer in C has reached the end. You can print the current position of the pointer through the Fil1.tell () function5 PrintBuff46Fil1.close ()#Close File

    Third, write the document
    You can create a file object that can be written by opening the file by means of ' w '. If the file exists then empty the file and write it, if the file does not exist then create.
    Example:

     1  fil = open ( " yyc.txt  , "  w   ", -1) #   write If the file exists, empty the file and then write, and if the file does not exist, create the  2  fil.write ( 123456hello   "   3  fil.flush () #   It's best to refresh the  4  fil.close () #   close file  

    Iv. read and write files r+, w+
    ' r ' mode open file created object can only read the contents of the file with the Read function, the ' W ' mode open file can only be written in the content. What if you want to write content and read content before a file is closed? This is what the ' r+ ' and ' w+ ' are capable of. However, it is recommended that you either read or write when a file is turned off, as this will cause strange problems, and you can read the following example carefully:
    ' w+ ' mode Example 1: If a write operation is performed between the open file and the closed file, then the last read is empty.

    1 #write first and then read2FIL5 = open ('Yyc.txt',"w+", 0)#both written and read if the file exists, empty the file first, and if the file does not exist, create3Fil5.write ('b')#Write First4Fil5.flush ()#It's best to refresh after writing .5BUFF2 =fil5.read ()#because the pointer points to the back of the write, the content is empty6 Print 'w+ mode First write after read:'+buff2#the content that is read is not covered, and this is empty because the pointer points to the back of the write, the end of the document7Fil5.close ()

    Example of ' w+ ' mode 2: If a read operation is performed before the file is opened and the file is closed, the read content is also empty.

    1 #Read and write first2FIL5 = open ('Yyc.txt',"w+", 0)#both written and read if the file exists, empty the file first, and if the file does not exist, create3BUFF2 =fil5.read ()#The content is empty because the file is emptied4 Print 'w+ mode First read and write:'+buff2#the content that is read is not covered, and it is not written, so it is empty.5Fil5.write ('123456')#6Fil5.flush ()#It's best to refresh after writing .7Fil5.close ()

    As can be seen from the above two examples, in the ' w+ ' mode to open the file and read and write operations, whether it is written after the first read or read after the write, read the content is empty , but the reason is different oh (the code comment is almost clear).
    ' r+ ' mode Example 1: If a write operation is performed between the open file and the closed file and then read, the content read is not overwritten after the write

    1 #write first and then read2FIL3 = open ('Yyc.txt',"r+",-1)#overwrite content both read and write from the beginning of the file3Fil3.write ('66666')4Fil3.flush ()#It's best to refresh after writing .5BUFF2 = Fil3.read ()#the read operation must be refreshed before6 Print 'r+ mode First write after read:'+buff2#the content that is read is not overwritten because the pointer points to the post-write position after the file is written, that is, the end of the document7Fil3.close ()

    Example of ' r+ ' mode 2: Run an error if you read and then write before opening the file and closing the file.

    1 #Read and write first (error)2 #fil3 = open (' Yyc.txt ', "r+", -1) #既读又写 overwrite content from file start location3 #buff2 = Fil3.read () #先读4 #print ' r+ mode read and write: ' +buff2 #输出的内容是覆盖后的内容, that is to say, he executed the write? It's weird, and it's an error5 ## Fil3.flush () #加不加效果一样6 #fil3.write (' 66666 ') #后写7 #Fil3.flush () #写完最好刷新一下8 #fil3.close ()

    V. Append write file a,a+
    If you want to keep the existing content of the file, just add the new content to the end of the original file or add a file content without affecting the original content, you can use a or a + mode.
    ' A + ' mode Example 1: If a write operation is performed before the file is opened and the file is closed, then the last read is empty.

    1 #write first and then read2FIL6 = open ('Yyc.txt','A +', 0)#Append If the file exists and create if the file does not exist3Fil6.write ('1234')#4Fil6.flush ()#It's best to refresh, to see if the file was successfully written.5 Print 'A + mode first write and then read:'+fil6.read ()#But the read is empty because the pointer points to the back of the write content6Fil6.close ()#Close File

    ' A + ' mode Example 2: If a read operation is performed before the file is opened and the file is closed, the content read is also empty.

    1 #Read and write first2FIL6 = open ('Yyc.txt','A +', 0)#Append If the file exists and create if the file does not exist3 Print 'A + mode reads and writes first:'+fil6.read ()#But the reading is empty.4Fil6.write ('1234')#5Fil6.flush ()#It's best to refresh, to see if the file was successfully written.6Fil6.close ()#Close File

    There are 6 examples, in fact, R+,w+,a+ has a similar point, that is, the location of the pointer to the file, determines the content of reading and writing . As long as you understand the position of the pointer in the file, it is not difficult to understand the file read and write.

  • Basic properties of a file

    The basic properties of the file are shown in the following example.
    Example:

    1 #-----------file Properties-----------2fil = Open ('Yyc.txt','a',-1)#Append3 PrintFil.mode#returns the access mode of the file being opened4 PrintFil.closed#is already closed5 PrintFil.name#returns the name of the file being opened6 PrintFil.softspace#

    Output:

    1 a 2 False 3 Yyc.txt 4 0
  • File pointer (Seek method)
    In front of the w+ mode of the first write after read, open the file is w+ mode, if the file exists then empty the file, if the file does not exist, create, before the file is written to find the pointer to write the contents of the back, then read the contents of the file, then read out the content is empty, we can through Seek () function to point the pointer to the beginning of the file before reading.
    Example:

    1FIL5 = open ('Yyc.txt',"w+", 0)#both written and read if the file exists, empty the file first, and if the file does not exist, create2Fil5.write ('PPPPPPPPP')#Write content First3Fil5.flush ()#It's best to refresh, note: At this point the pointer is at the end of the document4Fil5.seek (0)#move the pointer to the beginning of the document5BUFF2 =fil5.read ()#The content is empty because the file is emptied6 Print 'w+ Mode First writes and then moves the pointer last read:'+buff2#What you read is what you just wrote.7Fil5.close ()

    Output:

    1 w+ mode first write and move the pointer last read: PPPPPPPPP
  • Deletion and renaming of files
    Note: You need to import the appropriate method in the OS package or OS Package
    1. Rename with Os.rename (Curret_file_name,new_file_name) method: Two parameters, current file name and new file name
    2. Delete files with Os.remove (file_name) method: A parameter that needs to be deleted by the file name

      
      


      
      


      

Python Basic Notes Series 11: standard input and output, file read/write, pointers, etc.

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.