Python-based file processing

Source: Internet
Author: User

#r mode, default mode, file does not exist error#w mode, file exists overwrite, file does not exist then create#a mode, the file does not exist is created, the file exists does not overwrite, the write content is appended to the way.#file processing-readf = open ('a.txt','R', encoding='Utf-8')#print (' First-read: ', F.read ()) #读取文件所有内容,#f.seek (0) #可移动光标, the specified number in parentheses is the specified byte, you need to note a Chinese corresponding 3 bytes#f.seek (0) #可移动光标, the specified number in parentheses is the specified byte, you need to note a Chinese corresponding 3 bytes#print (' Seconde-read: ', F.read ()) #读取文件所有内容#print (f.read) #括号内可指定读取字符所在的位置. #print (F.readline (), end= ") #打印光标所在的当前行#print (F.readlines (), end= ") #文件内的所有字符, printed in a single line in the form of a list#file Processing-write#if the corresponding path does not have the file, a new file is created, if any, the contents of the file are emptied, and the new content is written#f = open (' B.txt ', ' W ', encoding= ' utf-8 ')#f.write (' 3333\n ') #只能以字符串的形式写数据#f.write (' 4444\n ')#print (' Data write complete! ')#f.writelines ([' 1111\n ', ' 2222\n ', ' 3333\n '])#print (' Data write complete! ')#print ()#f.close ()#file Handling--open#f = open (' A.txt ', ' a ', encoding= ' utf-8 ')#f.truncate (3) #截断#file Processing-Other#f = open (' B.txt ', ' W ', encoding= ' utf-8 ')#f.write (' ASFSADFGW ')#F.flush () #立即把内存的数据刷到硬盘去#f.close () #关闭文件#print (F.close ()) #判断文件是否关闭#print (f.readable ()) #判断文件是否可读#print (f.writable ()) #判断文件是否可写#f.seek (0)#print (F.tell ()) #输出当前光标位置#print (F.read ()) #打印全文#f.name,f.encoding#f = open (' A.txt ', ' W ', encoding= ' utf-8 ')#f.truncate (#括号内指定写入字符数)#file Processing--#f = open (' B.txt ', ' W ', encoding= ' utf-8 ')#f.write (' 44444\n ')#f.write (' 55555\n ')#f.close ()#Supplement#f = open (' a.txt ', ' RB ')#print (F.read ()) #以二进制的方式读取#print (F.read (). Decode (' Utf-8 ')) #将二进制转换为字符串, followed by an annotation code method#f=open (' a.txt ', ' WB ')#f.write (' Hello, Uncle '. Encode (' Utf-8 '))##F.A

Python-based file processing

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.