Python: file reading and writing
file reading and writing
<1> Write data (write)
Use write() to complete writing data to the file
demo: Create a new file file_write_test.py and write the following code into it:
f = open('test.txt', 'w')
f.write('hello world, i am here! ' * 5)
f. close()
After running, a file test.txt will be created in the path where the file_write_test.py file is located, and the content will be written. The running effect is as follows:
image.png
Notice:
If the file does not exist, create it; if it exists, clear it first, and then write data
<2> Read data (read)
Use read(num) to read data from the file, num indicates the length of the data to be read from the file (in bytes), if no num is passed in, it means to read the data in the file all data
demo: Create a new file file_read_test.py and write the following code into it:
f = open('test.txt', 'r')
content = f.read(5) # read up to 5 data
print(content)
print("-"*30) # dividing line for testing
content = f.read() # Continue to read all the remaining data from the last read position
print(content)
f.close() # close the file
Operating phenomenon:
hello
------------------------------
world, i am here!
Notice:
If you use "r" to open the file, you can omit open('test.txt')
<3> Read data (readlines)
readline can read the contents of the entire file at one time by line, and return a list, where each line is an element of the list.
f = open('test.txt', 'r')
content = f. readlines()
print(type(content))
for temp in content:
print(temp)
f. close()
readline() reads a line of data
pointer positioning
The tell() method is used to display the current pointer position
f = open('test.txt')
print(f.read(10)) # read specifies the number of bytes read
print(f.tell()) # tell() method displays the text where the current file pointer is located
f. close()
The seek(offset,whence) method is used to reset the position of the pointer.
offset: Indicates the offset
whence: Only one number in 012 can be passed in.
0 means start from the beginning of the file
1 means start from the current position
2 means start from the end of the file
f = open('test.txt','rb') # Need to specify the open mode as rb, read-only binary mode
print(f. read(3))
print(f. tell())
f.seek(2,0) # start from the beginning of the file, skip two bytes
print(f. read())
f.seek(1,1) # Start from the current position, skip a byte
print(f. read())
f.seek(-4,2) # Start from the end of the file, skip four bytes forward
print(f. read())
f. close()
<1> Write data (write)
Use write() to complete writing data to the file
demo: Create a new file file_write_test.py and write the following code into it:
f = open('test.txt', 'w')
f.write('hello world, i am here! ' * 5)
f. close()
After running, a file test.txt will be created in the path where the file_write_test.py file is located, and the content will be written. The running effect is as follows:
image.png
Notice:
If the file does not exist, create it; if it exists, clear it first, and then write data
<2> Read data (read)
Use read(num) to read data from the file, num indicates the length of the data to be read from the file (in bytes), if no num is passed in, it means to read the data in the file all data
demo: Create a new file file_read_test.py and write the following code into it:
f = open('test.txt', 'r')
content = f.read(5) # read up to 5 data
print(content)
print("-"*30) # dividing line for testing
content = f.read() # Continue to read all the remaining data from the last read position
print(content)
f.close() # close the file
Operating phenomenon:
hello
------------------------------
world, i am here!
Notice:
If you use "r" to open the file, you can omit open('test.txt')
<3> Read data (readlines)
readline can read the contents of the entire file at one time by line, and return a list, where each line is an element of the list.
f = open('test.txt', 'r')
content = f. readlines()
print(type(content))
for temp in content:
print(temp)
f. close()
readline() reads a line of data
pointer positioning
The tell() method is used to display the current pointer position
f = open('test.txt')
print(f.read(10)) # read specifies the number of bytes read
print(f.tell()) # tell() method displays the text where the current file pointer is located
f. close()
The seek(offset,whence) method is used to reset the position of the pointer.
offset: Indicates the offset
whence: Only one number in 012 can be passed in.
0 means start from the beginning of the file
1 means start from the current position
2 means start from the end of the file
f = open('test.txt','rb') # Need to specify the open mode as rb, read-only binary mode
print(f. read(3))
print(f. tell())
f.seek(2,0) # start from the beginning of the file, skip two bytes
print(f. read())
f.seek(1,1) # Start from the current position, skip a byte
print(f. read())
f.seek(-4,2) # Start from the end of the file, skip four bytes forward
print(f. read())
f. close()
Related Articles
-
A detailed explanation of Hadoop core architecture HDFS
Knowledge Base Team
-
What Does IOT Mean
Knowledge Base Team
-
6 Optional Technologies for Data Storage
Knowledge Base Team
-
What Is Blockchain Technology
Knowledge Base Team
Explore More Special Offers
-
Short Message Service(SMS) & Mail Service
50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00