Python Basic Learning-file manipulation

Source: Internet
Author: User

#__author: "Feng Lin"#Date:2018/8/27#pycharm using UTF-8 encoding format by default#file Operations#Head teacher. txt#1. File path: d:\. txt#2. Encoding method: UTF-8,GBK ...#3. Operation mode: Read-only, write-only, append, read/write, read#Note: Files stored in what encoding mode will be opened for Operation#Read-only#F=open (' head teacher ', mode= ' R ', encoding= ' utf-8 ')#Context=f.read ()#Print (context)#f.close ()#Open Non-text file, the sample is open with RB mode, bytes type open#F=open (' head teacher ', mode= ' RB ')#Context=f.read ()#Print (context)#f.close ()#Write only#for Mode=w, no files are created#f=open (' Log ', mode= "w", encoding= "Utf-8")#f.write ("Hello, world! ")#f.close ()##mode =w, if the file exists, it clears the contents of the source file, overriding#f=open (' Log ', mode= "w", encoding= "Utf-8")#f.write ("Hello, world!") ")#f.close ()##f=open (' Log ', mode= ' WB ')#f.write (' Hello, world! '. Encode (' UTF '))#f.close ()#F=open (' head teacher ', mode= ' a ', encoding= ' utf-8 ')#f.write (' hehe ')#f.close ()##以bytes类型追加#F=open (' head teacher ', mode= ' ab ')#f.write (' hehe '. Encode (' Utf-8 '))#f.close ()#read/write mode mode=r+ Note cursor position#F=open (' head teacher ', mode= ' r+ ', encoding= ' utf-8 ')#print (F.read ())#F.write (' Dream, xx ')#print (F.read ())#f.close ()#read-write mode mode=r+b open read/write with bytes type#F=open (' head teacher ', mode= ' r+b ')#print (F.read ())#f.write (' Dharma, qingming ' encode (' Utf-8 '))#f.close ()#read-write mode, mode=w+#f=open (' Logs ', mode= ' w+ ', encoding= ' utf-8 ')#f.write ("Big Frog in the event")#f.seek (0)#print (F.read ())#f.close ()#additional readable mode, mode=a+#f=open (' Logs ', mode= ' A + ', encoding= ' utf-8 ')#f.write ("slightly")#f.seek (0)#print (F.read ())#f.close ()#functionF=open ('logs', mode='r+', encoding='Utf-8')#Content=f.read (3) reads the minimum characters .F.seek (3)#is the position of the cursor according to the byte#F.tell () tells you where the cursor is.Print(F.tell ())#f.readable () returns the bool value to determine if it is readableContent=F.read ()Print(content)#ReadLine () one line readLine=F.readline ()Print(line)#ReadLines () treats each row as an element in the list and adds it to the listf.seek (0) lines=F.readlines ()Print(lines)#can act as a list forIinchlines:Print(i)#With open can not write close ()With open ('Log', mode='r+', encoding='Utf-8') as F1, open ('Log', mode='r+', encoding='Utf-8') as F2:Print(F1.read (), F2.readlines ())

Python Basic Learning-file manipulation

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.