Python full stack development---file processing

Source: Internet
Author: User

Python file processing

Recently a lot of students say that will not be the processing of documents, here to summarize the knowledge of file processing points.

One, open the file, close the file

The operation of the file takes the method of open ()

file = open (filename,mode,encoding) #打开文件 file.close () #关闭文件           

which

filename: The file path to open,mode: Open the file,encoding: Open the encoding format of the file (generally using utf-8)

To open the file's mode:

Note:filename is the path to the file, when you do not have a path to write only a file name, the default is created under the current folder, when you use the absolute path, remember the translation path of ' \ '

f = open (file='1.txt', mode='w', encoding='  Utf-8')  # in the current folder create # Note absolute path f = open (file='  c:\\users\\administrator\\desktop\\1.txt', mode='w' , encoding='utf-8')
Second, the commonly used methods
#Read the filef = Open (file='1.txt', mode='R', encoding='Utf-8') Data=F.read ()Print(data) f.close ()#Looping through Filesf = Open (file='1.txt', mode='R') forLineinchF:Print(line)#Write a filef = Open (file='2.txt', mode='W', encoding='GBK') F.write ('learn python.') F.close ()#Append mode This pattern is the last to continue writing at the end of the file that existsf = Open (file='2.txt', mode='a', encoding='GBK') F.write ('learn java.') F.close ()#read/write modef = Open (file='2.txt', mode='r+', encoding='GBK') Data=F.read ()Print(data) F.write ('\ n Learn c.') F.close ()#read-write modef = Open (file='2.txt', mode='r+', encoding='GBK') F.write ('\ n Xinjiang.') Data=F.read ()Print(data) f.close ()

#Other featuresf = Open (file='C:\\users\\administrator\\desktop\\1.txt', mode='r+', encoding='GBK') F.fileno ()#returns the index value of the file handle at the kernel, which is used for IO multiplexingF.flush ()#forcibly flush files from memory buffer to hard diskF.readable ()#interpret whether the file is readableF.readline ()#read-only line encountered \ r cutoffF.tell ()#returns the current cursor positionF.seek ()#move cursor to specified positionF.seekable ()#determine if a file is available for seek operationF.truncate ()#truncates a file by a specified length * Specifies the length, from the beginning of the truncation, do not specify the length, from the current position to the end of all removedF.writable ()#determine if it can be written

Iii. Modification of documents

Here's a simple way to implement a file modification:

#File ModificationImprot os #这里导入了os模块去改文件名f_name='C:\\users\\administrator\\desktop\\3.txt'F_new_name='%s.new'%F_nameold_str='Xinjiang'New_str='Beijing BJ'F= Open (File=f_name, mode='R', encoding='GBK') F_new= Open (File=f_new_name, mode='W', encoding='GBK') forLineinchF:ifOld_strinchLine:line=line.replace (OLD_STR,NEW_STR) f_new.write (line) os.rename (F_new_name, F_name)
Iv. using the WITH Operation file

Use the WITH statement so that you do not need to manually close the file.

# Open with the WITH statement, do not need to write F.close () withopen ('1.txt','r', encoding='utf-8') as F:    Print(F.read ())

First into the blog park, write bad please correct, such as found problems, I will timely correction, common progress.

Python full stack development---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.