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

Python file common functions read file write file locate file

編輯:Python

List of articles

    • 1. Common functions
      • 1.1.open function
      • 1.2. Read mode
    • 2. Reading documents
      • 2.1 Open directly
      • 2.2.read() Read
      • 2.3.readlines() Read
      • 2.4.readline() Read
    • 3. Writing documents
    • 4. Close file
    • 5. File location
    • 6. Author Q & A

Reading and writing files is the most common IO operation .Python Built in functions for reading and writing files , Usage and C Is compatible .

1. Common functions

1.1.open function

file object = open(file_name [, access_mode][, buffering])
file_name:file_name A variable is a string value that contains the name of the file you want to access .
access_mode:access_mode Determines the mode of opening files : read-only , write in , Supplemental . All available values are shown in the complete list below . This parameter is optional , The default file access mode is read-only (r).
buffering: If buffering The value of is set to 0, There will be no deposit . If buffering Value of 1, When you access a file, you will deposit lines . If you will buffering The value of is set to be greater than 1 The integer of , It shows that this is the buffer size of the deposit area . If you take a negative value , The buffer size of the deposit area is the system default .

1.2. Read mode

Pattern describe 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 .

2. Reading documents

2.1 Open directly

with open('filepath','r') as f:
for line in f:
print(line)
print(' A line of data ')

although f Is a file instance , But you can cycle through each row in the above way , Each line is a string when processing str, And this is the fastest and simplest way .

2.2.read() Read

fileObject.read([count])
The parameter passed is the count of bytes to read from the opened file . This method reads in from the beginning of the file , If no incoming count, It will try to read as much as possible , Probably to the end of the file .

with open('filepath','r') as f:
ff=f.read()

This function reads the entire contents of the file into a string at one time . It's the kind of lump , If you put ff Output by loop reading , It will be a character , because ff Is string , Essentially, tuple.

2.3.readlines() Read

with open('filepath','r') as f:
lines=f.readlines()
for line in lines:
print(line)

2.4.readline() Read

with open('filepath','r') as f:
line =f.readline()
while line:
print(line)
line=f.readline()

This is a line by line reading , Very memory saving , It's good when the file is huge .

3. Writing documents

write() Method to write any string to an open file . It's important to pay attention to ,Python Strings can be binary data , Not just words .

fileObject.write(string)

# Open a file 
fo = open("foo.txt", "w")
fo.write( "\nVery good site!\n")
# Close open files 
fo.close()

4. Close file

File Object's close() Method to refresh any information in the buffer that has not been written , And close the file , After that, no more writes can be made .

fileObject.close()

# Open a file 
fo = open("foo.txt", "w")
print " file name : ", fo.name
# Close open files 
fo.close()

5. File location

tell() Method tells you the current location in the file , let me put it another way , The next read and write will happen after so many bytes at the beginning of the file .
seek(offset [,from]) Method to change the location of the current file .Offset Variable represents the number of bytes to move .From Variable to specify the reference location to start moving bytes .
If from Is set to 0, This means that the beginning of the file is used as a reference location for moving bytes . If it is set to 1, Then use the current position as the reference position . If it's set to 2, Then the end of the file will be used as a reference location .

# Open a file 
fo = open("foo.txt", "r+")
str = fo.read(10)
print " The string read is : ", str
# Find the current location 
position = fo.tell()
print " Current file location : ", position
# Relocate the pointer to the beginning of the file again 
position = fo.seek(0, 0)
str = fo.read(10)
print " Reread string : ", str
# Close open files 
fo.close()

6. Author Q & A

If you have any questions , Please leave a message .


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