--open of Python built-in functions

Source: Internet
Author: User

OPEN1, the Open function, which is used to process the file 2, open ("file name, mode (in what way), encode") A, basic open mode

        

#1, read-only, Rf = open ("Ha.log","R")#2, write only, w,[file is not readable, the file does not exist, it is created; empty content]f = open ("Ha.log","W") F.write ("123") F.close ()#3, X, write-only mode "unreadable; not exist then create, present error"f = open ("Ha1.log","x") F.write ("456") F.close ()#4. Append mode "unreadable; not exist" create; append content only; "f = open ("Ha1.log","a") F.write ("666") F.close ()
B, Bytes open the way
#1, read-only, RBf = open ("Ha.log","RB") Data=F.read () f.close ( )Print(Type (data))#2. Write onlyf = open ("Ha.log","WB") f.write (bytes ("China", encoding="Utf-8") ) F.close ()
C, Difference:
# Normal Open # Normal Open has been internal Python operation, Direct is the UTF-8,RB way you need to convert # ===python internally converts 101010 = = to a string, using string manipulation # 010101 + =  python interpreter  + programmer # binary Open Way #RB Way open only get 2 binary code , you need to convert yourself into utf-8.
D, "+" means you can read and write a file at the same time
# r+, read and write "readable, writable" # w+, write "readable, writable" # x+, write "readable, writable" # A +, write "readable, writable"
1, r+
# R, append at the end, move the pointer to the last f = open ("ha.log","r+", encoding= " Utf-8 " # pointer is 0, start position #f.close ()
#从开始向后读
#写, append, pointer to last
2, w+,x+
#w+, clear the contents of the file, then write, you can read#first clear, then write to read, write, pointer to the lastf = open ("Ha.log","w+", encoding="Utf-8") F.write ("Wu") F.seek (0)#pointer pointing to 0data =F.read () f.close ( )Print(data) #x+ Basic Ibid. if file exists, then error
3. A +
A + A, open at the same time, the pointer has reached the end, the contents of the file can not be read # Write, will be appended, the pointer is put to the last f = open ("ha.log" ,"a+", encoding="utf-8"=  F.read () f.close ()print(data)
3, the operation function of the file
#===============================#F.tell () #获取指针的位置#f.seek (num) #调整指针的位置#===============================#Close file#Flush Force flush file Internal buffer#Read Reading file#ReadLine reading a line#pointer position in seek file#Tell to get the position of the pointer#truncate truncates data, leaving only the data before the pointer is specified#Write Read file

4, read the contents of each line

f = open ("ha.log","R", encoding="utf-8 " ) for in F:     print(line)  =#  F.readline, you can read the contents of all rows

5. With open

# When the with code block finishes executing, the file resource is automatically closed and freed internally. with open ("ha.log","R") as F:    F.read () # python inside will help you shut down without writing F.close

 

#open 2 files at a timeWith open ("Log1","R") as Boj1, open ("log2","R") as Obj2:Pass#open 2 files and transfer the dataWith open ("source File","R") as Obj1,open ("New File","W") as Obj2: forLineinchObj1:obj2.write (line)#Open a source file while the content is written in a new file

6. Lambda expression

# Lambda can replace a simple function def F1 ():     return 123Lambdadef  F3 (A1,A2):     return A1 +  Lambda a1,a2:a1+=print=print(R2)

    

--open of Python built-in functions

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.