程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Self taught Python 36 file operation and processing

編輯:Python

Python File operation processing


List of articles

  • Python File operation processing
  • One 、 File operating system


In computer system , The length of time the underlying information is stored , It can be divided into temporary information and permanent information . Simply speaking , Temporary information is stored in the temporary storage device of the computer system ( For example, stored in computer memory ), Such information is lost when the system is powered off . Permanent information is stored in a permanent storage device of a computer ( For example, stored on disk and optical disc ). The smallest permanent storage unit is the file , So file management is an important problem in computer system .


One 、 File operating system

stay Python There is a very important knowledge point in : File operations . To realize file operation, you need to master not only the basic grammar , You also need to be proficient in function functions , Because the file processing is realized through the corresponding function .
In the computer world , Text files can store a variety of data information , For example, heaven
Gas forecast 、 Traffic information 、 Financial data 、 Literary works, etc . When you need to analyze or modify information stored in a file , Reading files is very important . Through the file reading function , You can get the contents of a text file , And you can reformat the data inside and write it to the file , And it can let the browser display the contents of the file .
Before reading the contents of a file , You need to open this file first . stay Python In the program , You can use built-in functions open(( To open files , And use relevant methods to read or write the contents of the file for program processing and use , You can also think of a file as Python A data type in . Using functions open() The syntax format of is as follows :

 open(file, mode = 'r', bufferung = -1, encoding = None, errors = None,
newline = None, closefd = True, opener = None)

When using the above functions open() After opening a file , A file object will be returned . The main parameters in the above format are described in the table below :

Parameter name describe file Indicates the file name to open mode Optional parameters , File open mode . This parameter is optional , The default file access mode is read-only bufering Optional parameters , Buffer size encoding File code type errors Coding error handling method newline Controls the behavior of the generic newline pattern closefd Controls whether files are completely closed when they are closed

In the above format , Parameters “mode" Indicates file open mode . stay Python In the program , The common file opening modes are shown in the following table .

Pattern describe r Open the file read-only . The pointer to the file will be placed at the beginning of the file . This is the default mode rb Open a file in binary format for read-only use . The file pointer will be placed at the beginning of the file r+ Open a file for reading / Write . The file pointer will be placed at the beginning of the file rb+ Open a file in binary format for reading / Write . The file pointer will be placed at the beginning of the file w Open a file only for writing . Overwrite the file if it already exists . If the file does not exist , Create a new file wb Opening a file in binary format is only used for writing . Overwrite the file if it already exists . If the file does not exist , Create a new file w+ Open a file for reading / Write . Overwrite the file if it already exists . If the file does not exist , Create a new file wb+ Open a file in binary format for reading 1 Write . Overwrite the file if it already exists . If the file does not exist , Create a new file a Open a file for appending . If the file already exists , The file pointer will be placed at the end of the file . in other words , The new content will be written after the existing content . If the file does not exist , Create a new file to write to ab Open a file in binary format for appending . If the file already exists , The file pointer will be placed at the end of the file . in other words , The new content will be written after the existing content . If the file does not exist , Create a new file to write to a+ Open a file for reading / Write . If the file already exists , The file pointer will be placed at the end of the file . Append mode when the file opens . If the file does not exist , Create a new file for reading / Write ab+ Open a file in binary format for appending . If the file already exists , The file pointer will be placed at the end of the file . If the file does not exist , Create a new file for reading / Write
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved