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

File open operation of Python notes

編輯:Python

#set( aggregate )
# Collection of unordered elements
# The element is the only , Duplicate elements are automatically filtered

#file Object opening and processing

01 Open file create file object

#open(file, mode=‘r’, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

fl = open('HelloWorld.txt','w',encoding='utf-8')
fl.write('Hello World')
fl.close()

File open type description :

  • t Text mode ( Default ).
  • x Write mode , Create a new file , An error is reported if the file already exists .
  • b Binary mode .
    • Open a file to update ( Can read but write ).
  • U Universal newline pattern ( Not recommended ).
  • 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 . This is the default mode . Generally used for non - text files such as pictures .
  • r+ Open a file for reading and writing . The file pointer will be placed at the beginning of the file .
  • rb+ Open a file in binary format for reading and writing . The file pointer will be placed at the beginning of the file . Generally used for non - text files such as pictures .
  • w Open a file only for writing . Open the file if it already exists , And edit from the beginning , The original content will be deleted . If the file does not exist , Create a new file .
  • wb Opening a file in binary format is only used for writing . Open the file if it already exists , And edit from the beginning , The original content will be deleted . If the file does not exist , Create a new file . Generally used for non - text files such as pictures .
  • w+ Open a file for reading and writing . Open the file if it already exists , And edit from the beginning , The original content will be deleted . If the file does not exist , Create a new file .
  • wb+ Open a file in binary format for reading and writing . Open the file if it already exists , And edit from the beginning , The original content will be deleted . If the file does not exist , Create a new file . Generally used for non - text files such as pictures .
  • 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 and writing . 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 and writing .
  • 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 and writing .
02 Read file contents

fl = open('HelloWorld.txt','r',encoding='utf-8')

  • file.read([size])
    If not specified, return the whole file , If the file size >2 Double the memory , An empty string is returned when the end of the file is read .
  • file.readline()
    Back to the line .
  • file.readlines([size])
    Returns a list of the specified number of rows , If not specified, all lines will be returned .
03 Write to the document
  • f.write(···)
    If you want to write data other than strings , First convert it to a string .
04 Get current pointer position ( Bytes to file header )
  • f.tell()
05 Move the pointer position
  • f.seek( Offset ,[ The starting position ])
    The starting position : 0 - The file header , The default value is ; 1 - The current position ; 2 - End of document
06 Close file
  • f.close()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved