[Chapter 8] Python File Operation

Posted by downfall on Sat, 07 Sep 2019 06:39:42 +0200

Introduction of methods

The Python open() method is used to open a file and return the file object. This function is needed to process the file. If the file cannot be opened, OSError will be thrown.

Note: When using the open() method, you must ensure that the file object is closed, that is, the close() method is called.

Grammatical Format of 1# open Function
2 
3 open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
4 file: Necessary, file path (relative or absolute path).
5 mode: Optional, file open mode
6 buffering: Setting buffers
7 errors: error level
8 newline: distinguish newline characters
9 closefd: Input file parameter type

The parameters of mode l are:

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.
w
Open a file for writing only. If the file already exists, it is overwritten. 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. That is to say, the new content will be written after the existing content. If the file does not exist, create a new file for writing.
rb
Open a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
r+
Open a file for reading and writing. The file pointer will be placed at the beginning of the file. It's safe to read and write first.
rb+
Open a file in binary format for reading and writing. The file pointer will be placed at the beginning of the file.
w+
Open a file for reading and writing. If the file already exists, it is overwritten. If the file does not exist, create a new file
wb+
Open a file in binary format for reading and writing. If the file already exists, it is overwritten. If the file does not exist, create a new file.
wb
Open a file in binary format for writing only. If the file already exists, it is overwritten. If the file does not exist, create a new file.
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. That is to say, the new content will be written after the existing content. If the file does not exist, create a new file for writing.
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. File opens in additional mode. 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.


Common methods in file:

Method
describe
file.close()
Close the file. Files cannot be read or written after closing.
file.flush()
Refresh the internal buffer of the file and write the data of the internal buffer directly to the file instead of passively waiting for the output buffer to write.
file.next()
Return to the next line of the file
file.read([size])
Read the specified number of bytes from the file, and read all if not given or negative
file.readline([size])
Read the entire line, including the " n" character.
file.readlines([sizehint])
Read all rows and return the list. Given sizeint > 0, the total is about sizeint.
In byte rows, the actual read value may be larger than sizhint because the buffer needs to be filled.
file.seek(offset[, whence])
Set the current location of the file
file.tell()
Returns the current location of the file.
file.truncate([size])
The intercepted bytes are specified by size and default is the current file location.
file.write(str)
Write a string to a file without returning a value
file.writelines(sequence)
Write a list of sequence strings to the file, and add line breaks for each line if you need to wrap.

II. Operation of Documents

2.1 Read files

2.1.1 read

 1 # There are two paths:
 2 # 1.Relative path, relative to the folder where the current program is located
 3 #    ../ Return to the previous directory
 4 #    Relatively to the folder where the current program is located
 5 # 2.Absolute Path
 6 #   * Find from the disk root directory
 7 #   * Absolute Path on Internet
 8 
 9 f = open("a.txt", mode="r", encoding="utf-8")
10 
11 # read The parameter of the function is how many bytes to read. If no parameter is added, the default is to read all the contents of the file once.
12 data = f.read()
13 # Print the contents of the read file
14 print(data)
15 
16 # Close files
17 f.close()

2.1.2 readlines

Just as read has no parameters, readlines can read the contents of the entire file in a row-by-row fashion, and return a list with each row of data as an element.

f = open("a.txt", mode="r", encoding="utf-8")

data = f.readlines()
# Print the contents of the read file
print(type(data))   # <class 'list'>

# Close files
f.close()

2.1.3 readline 

Read a row of data

f = open("a.txt", mode="r", encoding="utf-8")

# Read only one row of data
data = f.readline()
# Print the contents of the read file
print(data)

# Close files
f.close()

2.1.4 Read large files

f = open("a.txt", mode="r", encoding="utf-8")

# Method of reading large files
for line in f:
    print(line)
# Close files
f.close()

Iterative traversal of iteratable object f: for line in f, buffered IO and memory management are automatically used without worrying about any large files.

2.2 Writing documents

1 # w Schema writing overwrites the source file
2 f = open("a.txt", mode="w", encoding="utf-8")
3 
4 f.write("aaa")
5 # Close
6 f.close()
1 # Append
2 f = open("a.txt", mode="a", encoding="utf-8")
3 f.write("ddd")
4 f.flush()
5 f.close()
 1 # r+ The default mode pointer is at the beginning of the file
 2 # f = open("Teacher roll call", mode="r+", encoding="utf-8")
 3 # s = f.read()
 4 # print(s)
 5 # f.write("Jay Chou")
 6 # f.close()
 7 # God pit
 8 f = open("Boutique", mode="r+", encoding="utf-8")
 9 s = f.read(3)  # Read 3 characters
10 # No matter how many you read in front of you, what you write in the back is at the end.
11 f.write("aabbcc")  # Write before there is no action, write at the beginning, and write after reading something, then at the end.
12 f.flush()
13 f.close()

Cursor movement in files

 1: read(3):
 2.1. When the file is opened in text mode, it represents reading three characters.
 3.2. When the file is opened in mode b, it represents Reading 3 bytes.
 42: The cursor movement in the rest of the files is in bytes such as seek, tell, truncate
 5     seek(offset[, whence])
 6 offset -- The starting offset, that is, the number of bytes that need to be moved, and if it is negative, the number of bytes that need to be moved from the bottom to the bottom.
 7 whence: Optional, default value is 0. Define a parameter for offset to indicate where to start offset; 0 to start at the beginning of the file, 1 to start at the current location, and 2 to start at the end of the file.
 8 
 9 
10 Notes:
11.1. seek has three modes of movement 0, 1, 2, of which 1 and 2 must be carried out in mode b, but in either mode, bytes are used as a single displacement.
12.2. truncate is a truncated file, so the way to open the file must be writable, but it can not be opened by w or w +, because it clears the file directly, so truncate should test the effect in r + or a + mode. 
13   

 

 1 #!/usr/bin/python3
 2  
 3 # Open the file
 4 fo = open("runoob.txt", "r+")
 5 print ("File name: ", fo.name)
 6  
 7 line = fo.readline()
 8 print ("The data read is: %s" % (line))
 9  
10 # Reset the file read pointer to the beginning
11 fo.seek(0, 0)
12 line = fo.readline()
13 print ("The data read is: %s" % (line))
14  
15  
16 # Close files
17 fo.close()

2.3 with operation

To avoid forgetting to close after opening a file, you can manage the context by:
2   with open('log','r') as f:
3 In this way, when the with code block is executed, the internal file resources are automatically closed and released.
4 
5 After Python 2.7, with also supports managing the context of multiple files at the same time.
6 with open('log1') as obj1, open('log2') as obj2:
1 with open("a.txt", mode="r", encoding="utf-8") as f:
2     data = f.read()
3     print(data)
# Prompt input file
oldFileName = input("Please enter the name of the file to be copied:")

# Open the file in a read manner
oldFile = open(oldFileName,'rb')

# Extract the suffix of the file
fileFlagNum = oldFileName.rfind('.')
if fileFlagNum > 0:
    fileFlag = oldFileName[fileFlagNum:]

# Organize new file names
newFileName = oldFileName[:fileFlagNum] + '[Duplicate]' + fileFlag

# create a new file
newFile = open(newFileName, 'wb')

# Copy data from old files, line by line, to new files
for lineContent in oldFile.readlines():
    newFile.write(lineContent)

# Close files
oldFile.close()
newFile.close()
File backup

2.4 Document-related operations

 1 import os
 2 
 3 # File renaming
 4 os.rename("Dissertation.txt", "Dissertation-Final Edition.txt")
 5 
 6 # Delete files
 7 os.remove("Dissertation.txt")
 8 
 9 # create folder
10 os.mkdir("abc")
11 
12 # Get the current directory
13 os.getcwd()
14 
15 # Change the default directory
16 os.chdir("../")
17 
18 # Get a list of directories
19 os.listdir("./")
20 
21 # remove folders
22 os.rmdir("abc")
# Create a new file, write the modified file to the new file, delete the old file, and rename the new file

import os

with open("Eating", mode="r", encoding="utf-8") as f1, \
        open("Eating_copy", mode="w", encoding="utf-8") as f2:
    for line in f1:
        new_line = line.replace("meat", "food")
        f2.write(new_line)


os.remove("Eating")     # Delete files
os.rename("Eating_copy", "Eating")    # rename file
Modify file content
#coding=utf-8
# Batch prefix file name
import os

funFlag = 1 # 1 Represents adding flag 2 to denote deleting flag
folderName = './renameDir/'

# Gets all file names for the specified path
dirList = os.listdir(folderName)

# Traverse to output all file names
for name in dirList:
    print name

    if funFlag == 1:
        newName = '[Dongge's products]-' + name
    elif funFlag == 2:
        num = len('[Dongge's products]-')
        newName = name[num:]
    print newName

    os.rename(folderName+name, folderName+newName)
Batch Modification of File Names

Topics: Python encoding REST