Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and write files by python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how python reads and writes files". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Python how to read and write files"!

Python's function to read and write files is open or file

file_handler = open(filename,,mode)

pattern describes

r Open the file in read mode to read the file information.

w Open file in write mode, write information to file. If the file exists, empty the file and write new content.

a Open the file in append mode (i.e., the file pointer automatically moves to the end of the file as soon as the file is opened), or create it if it does not exist

r+ Open files in read-write mode, and can read and write files. ## r+, if f.read() is not first written, the newly written content will overwrite the write

w+ Erases the file contents and then opens the file read-write.

a+ Open the file read-write and move the file pointer to the end of the file.

b Open files in binary mode instead of text mode. This mode is only valid for Windows or Dos, Unix-like files are operated in binary mode.

method describes

f.close() Close the file, remember to close it after opening the file with open(), otherwise it will occupy the system's number of open file handles.

f.fileno() Gets the file descriptor, which is a number

f.flush() Flushes the output buffer

f.isatty() returns True if the file is an interactive terminal, False otherwise. f.read ([count]) reads the file, and if there is count, reads count bytes.

f.next () reads one line and shifts the file action marker to the next line. When the file is finished, return to operation.

f.readline() reads a line and returns it as a string. Empty to finish this line.

f.readlines() reads all the lines, that is, reads the information of the entire file, and returns a list that can be traversed.

f.seek (offset[,where]) Moves the file pointer to the offset position relative to where. where 0 indicates the beginning of the file, which is the default;1 indicates the current position;2 indicates the end of the file.

f.tell() Gets the file pointer location.

f.truncate([size]) intercepts the file so that the file size is size.

f.write(string) Write string to file. Whether or not the original contents of the file are erased before writing depends on the file open mode.

f.writelines(list) Write the string in the list to the file line by line, continuously write to the file, no newlines. Whether or not the original contents of the file are erased before writing depends on the file open mode.

At this point, I believe that everyone has a deeper understanding of "how python reads and writes files," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report