Software testing/test development丨Python built-in library file processing learning notes sharing

File operation steps

  • open a file
  • Manipulate files: read/write content
  • Close the file (reading and writing are completed, and must be closed in time)

open method

def open(file, mode='r', buffering=None, 
encoding=None, errors=None, newline=None, 
closefd=True):

File read and write mode

image

Read operation

image

The dangers of forgetting to close a file

  • Opening a certain number of files will cause the opening to fail.
  • It takes up memory space and is a waste of resources.
  • Will cause the system to automatically reclaim resources and lose data

with usage

with open('data.txt', 'r', encoding='utf-8') as f:
    print(f.read())
print(f.closed)   ## 查看关闭状态

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you! 

Guess you like

Origin blog.csdn.net/2301_78276982/article/details/135297241