Python File Reading and Writing

python file read and write

Reading and writing files is the most common IO operation. Python has built-in functions for reading and writing files, which are compatible with C.

  • Before we read and write files, we must understand that the function of reading and writing files on disk is provided by the operating system. Modern operating system does not allow ordinary programs to operate the disk directly. So, reading and writing files is to request the operating system to open a file object (usually called a file descriptor), and then, through the interface provided by the operating system, from this file object. Read data (read files), or write data to the file object (write files).

# File operations, create file objects, create file objects through the open() function
mode: Represents the type of operation to be performed by the file
r: read mode
w: Write mode (the contents of the file are emptied before writing each time, and then rewritten)
A: Add mode (file content is not emptied every time a file is written)

import time;
#Character encoding for encoding=utf-8 file operation
#read only reads by reading everything in the file (not applicable)
print('The contents of the document are as follows:%s';
#readline reads the contents of a line (applicable)
print('The first line in the file is:%s'%file.readline())
#readlines reads the contents of a line and generates the contents of each line in the file as an element in the list (not applicable)
for line in lists:
    print("The content is:%s"%line)
#When you have finished manipulating the file objects of your teammates, you must close the file objects.
file.close();#When writing to the mode, the close method is not executed and the content is not persisted to the file.
#If you don't write close, the default interpreter automatically closes the file for you, but it will consume more interpreter performance.

# Write mode (w): Write content, emptying the original content of the file


# Writing method

file1.write('hiahia is hiahia')
time.sleep(10)#python works on a single thread, hibernates the current thread, and delays the execution of the close method
file1.close();#In python programs, if the user does not manually close the file object, python will automatically close you at the bottom.
print('The dormancy is over!')

# Add mode (a): Write content without modifying the original content of the file

file2.write("\n Mountain wind");

# Trouble: Call close every time you create a file object
# The with statement automatically closes the file object at the end

with open('HelloWorld','r',encoding='utf-8') as file:

# There is no need to close the file object at this time!

# r + (read-write mode) w + (write-read mode) a + (additional read mode)

# Reading and Writing Mode: It can write as well as read. Reading is the main method and writing is the supplement.

with open('hiahia','r+',encoding='utf-8') as file:
    print( file content
    file.write('hehe')#Write-and-read mode places the written content at the end of the file content#Overlay the original content of the file from the beginning of the file

# Write-Read Mode: Readable-Writable, Write after Clearing Files

with open('hiahia','w+',encoding='utf-8') as file:
    file.write('hello python!')
    file.write('\nhello python!')
    #The seek method is used to move the handle 0 of the file to represent the starting position in the handle.

# Additional Reading Mode: The most appropriate mode for both writing and reading, appended at the end of the document

with open('hiahia','a+',encoding='utf-8') as file:
    #tell queries the value number of the current file handle

Method of File Operation

# tell: Get the location of the current file pointer (record the location of the current file pointer when an exception occurs to the transfer)
# See: Set the breakpoint of the current file pointer (see relocation file (tell) at the beginning of the continuation). If one of the two hosts fails during the process of transferring files, the file will stop transferring, then the file will be transferred again when the program returns to normal.
# Do you need to retransmit the file to 500MB
# Turcate: Truncate the file and empty the file

with open('hiahia','r',encoding='utf-8') as file:
    print('Default location of file:%s'%file.tell())
    print( file content
    print('The position of the pointer now:%s'%file.tell())

# The function of turncate method in write mode or read-write mode

with open('HelloWorld','r+',encoding='utf-8') as file:

Keywords: Python encoding

Added by kf on Sat, 18 May 2019 06:19:51 +0300