python thirteen: package and file operations

Package and file operations:

1. What is a bag

Python package is used to manage the project in a special file py file folder, this folder there is a special file: the init .py
(ordinary files in the project folder for general management, non-code files desired item)

2. How to use the contents of the package-import

import package name-after importing, you can use'package name.' to use all global variables defined in the _inte__.py file in this package

import package name. module name-after importing, you can use'package name. module name.' to use all global variables in the specified module

from package name import module name-import the specified module in the specified package, after importing, you can use'module name.' to use all global variables in the module

from package name. module name import variable 1, variable 2,...-Import the specified variable in the specified module in the specified package, and use the variable directly when you use it

Data persistence:

The data is saved in the running memory by default. After the program ends, all the data in the running memory will be automatically destroyed

If the data is not destroyed, it needs to be saved in the hard disk

Saving data to the hard disk is data persistence. (Note: Data cannot be placed directly on the hard disk, it must be saved in a file)

Common types: txt, json, plist, database

1. File operation: open file -> operate file (read operation, write operation) -> close file

open (file location, operation type, encoding = utf-8)

  • path:

Absolute path: the full path of the file in the computer

Relative path:.-Indicates the current directory (the current directory refers to the directory where the file that currently writes the code to open the file is located)
…-Indicates the upper directory of the
current directory...-Indicates the upper directory of the upper directory of the current directory

  • Operation type:

Values ​​that determine the read and write mode: r, w, a
r-read only
w-write only; the content in the original file will be cleared after opening
a-write only; the content in the original file will not be cleared after opening

Determine the data type of the operation: t (default value), b
t-the content read and the content written to the file are of string type
b-the content read and written to the file are of type bytes (binary)

Note: When opening the file, the mode must be selected from each of these two sets of values. If the second set of values ​​is not selected, it means t is selected.

open a file

Open a file that does not exist in read mode, an error will be reported

Open a non-existent file in the form of writing, no error will be reported, and the non-existent file will be created automatically

Close file

1) File object.close()

2) with open (file location, operation type, encoding = utf-8) as file object:

​ File scope

Operating file

1) Read operation

  • File object.read()-start from the file read and write position, read to the end of the file
    • seek(0) # Move the read and write position to the beginning of the file

Reading and writing of binary files

with open('./sources/luffy.jpg', 'rb') as f:
    result = f.read()

with open('./sources/b.jpg', 'wb') as f:
    f.write(result)
  • File object.readline()-read a line (starting from the reading and writing position, reading to the end of the line); can only be used for reading text files (each line is a list)

2) Write operation

File object.write(data)

effect:

a. Append new content at the end of the file

with open('sources/aaa.txt', 'a', encoding='utf-8') as f:
    f.write('\n==================')

b. Add new content at the beginning of the file

with open('sources/aaa.txt', encoding='utf-8') as f:
     result = f.read()
     print(result)

c. Modify the'bed' in the original file to'bed'

with open('sources/aaa.txt', 'w', encoding='utf-8') as f:
     result = result.replace('床', 'bed')
     f.write(result)

Guess you like

Origin blog.csdn.net/SaharaLater/article/details/111655543