2020_10_15_Package and file operations

package

1. The folder containing the __init__.py file is the package. The
package is used to classify or encapsulate py files (modules)

2. Use the module in the package
1) import package name. module name
package name. module name. variable

2) Import package name. Module name as new name
Rename the "package name. module name" and use the variable in the way of "new name. variable".

3) from package name import module name 1, module name 2, module name 3, ...
module name. Variable

4) from package name import module name 1 as new module name 1, module name 2, module name 3,...

5) from package name. module name import variable name 1, variable name 2, variable name 3,...

3. The __init__.py file of the customized package
"""
No matter what method is used to import the package or the contents of the package, the system will first execute the __init__.py file
"""

Data persistence

1.1) Computer storage of data The data in the
program is stored in the running memory by default, and the data saved in the running memory will be automatically destroyed after the program ends.
The data saved in the disk will always exist unless manually deleted or the disk is damaged.

2) Persistence
Save the data in the program to the disk through files. This process is the process of data persistence.
Persistence tools: ordinary text files (.txt), database files (.sqlite, .db), json files, plist files, csv files, excel files, etc.

2. File operation (operating file content)
"""
Basic steps for operating a file: open file -> read/write operation -> close file
"""
3. python realize file operation
1) open
"""
open(file, mode ='r', …, encoding=None)
-Open the specified file in the specified way and return (the file object is returned) 1.file-string, the path of the file to be opened.
Path:
absolute path-the file is on the computer Mid-full path
Relative path-Use. To replace the part of the full path of the file, write only part of the path;
.-Indicates the current directory (the directory where the current code file is located),
can be omitted...-
Indicates the upper directory of the current directory...-
Indicates the current directory The upper directory of the upper directory

2.mode-string, opening method (decide what operations the file can support after opening the file; decide the type of data read and write)
'r'-means read-only (default)
'w'-means write only, it will be opened first Empty file'a'
-means write only, the original file content will be retained when opened

       't'  -  表示文本数据,对应str类型(默认)
       'b'  -  表示二进制数据,对应bytes类型
       
       'rt'/'tr'/'r'
       'wt'/'tw'/'w'
       'at'/'ta'/'a'
       'rb'/'br'
       'ab'/'ba'
       'wb'/'bw'  
       注意:
       1) 以读的形式打开一个不存在的文件会报错;
          以写的形式打开一个不存在的文件不会报错,并且会自动创建 
       2) 如果是文本文件打开的时候可以是'b'也可以是't';
          如果是非文本文件打开的时候只能带'b'      

encoding-Set the encoding method of the text file, generally use'utf-8'.
Note:
1) The encoding method corresponding to the creation of the file and the opening of the file, reading and writing must be the same.
2) If the file is opened with'b ', encoding cannot be set

3. Read and write operations
1. Read
file objects . read ()-read from the read and write position to the end of the file (applicable to all files)
file objects. readline()-read from the read and write position to the end of a line (only available (For text files opened with't')

2. Write
file object . write (data)-write the specified data into the file (write from the read and write position)

File object.seek(0)-move the reading and writing position to the beginning of the file (the reading and writing position is opened by default at the beginning of the file in read mode, and the reading and writing position is opened at the end of the file by default)

Data persistence

1) Create a file to save the data that needs to be persisted
2) Get the data from the file when you need this data
3) If you modify the data in the program, you need to update the latest data to the file

f = open(‘files/count.txt’)
num = int(f.read())
num += 1
print(num)

f = open(‘files/count.txt’, ‘w’)
f.write(str(num))
f.close()

Exercise 2: Add students
a. Add a student each time the program is run, and print all the students that have been added
b. Add a student each time the program is run, and print all the students that have been added
stu1 in the form of a list -> stu1 ['stu1']
stu2 -> stu1 stu2 ['stu1','stu2']
Xiaoming -> stu1 stu2 Xiaoming['stu1','stu2','小明']

a:
name = input('input student name:')
f = open(r'test_directory\practice.txt','a')
f.write(f' {name}')
f = open(r'test_directory\practice .txt')
result = f.read()
print('Current students have:', result)

b:
name = input(‘输入学生名字:’)
f = open(r’test_directory\practice.txt’)
result = f.read()
stu = eval(result)
stu.append(name)
f = open(r’test_directory\practice.txt’, ‘w’)
f.write(str(stu))
f = open(r’test_directory\practice.txt’)
result = f.read()
print(result)

Guess you like

Origin blog.csdn.net/xdhmanan/article/details/109104205