python11. file

Basic file operations:
black screen and flashback are likely to be due to memory overflow, insufficient memory temporarily used by the memory stick, memory is temporary storage, and the hard disk can be stored persistently. The
memory stick is a medium speed for cpu and ssd hard disks.

Use python to manipulate files.
Relative path:
you can access it in the current path. You don’t need to enter the path name, just enter the file name.
Absolute path: If you
don’t access under the same path (scripts under the same path), you can add the full path name. Add r to cancel character escaping, starting from C drive

File open mode:
add the file name in parentheses, the file exists in read-write mode ('r','a', etc.)
, but a blank line will be printed out if there is no content
in the file. The file pointer here refers to the cursor

Read/write file:
point to name. Mode()
readline() read one line, more can read a few more lines
readlines() returns the contents of multiple lines as elements of the list, each element is also followed by \n
Means newline writelines() to write multiple lines and add \n between each line (concise method: use a variable to assign multiple lines of content without directly inputting the parentheses, add · · · to indicate the meaning of line break)
flush () refresh, take the initiative to save The meaning of
time.sleep(10) means that the execution of this step will stop for 10 seconds and then start to execute
tell() uses a number to indicate the position of the cursor, \n it is hidden and counts the position of two numbers,
seek() plus a number Can move the cursor

IO stream: (the meaning of input and output) is not stored in the real file, but a fake file that exists temporarily, so the fake file is not saved when the close() method is called, but will be lost. The
module is used before use, import io
xx= io.StringIO() creates a character stream (create a binary stream, change String to Bytes)
xx.write() write
print(xx.getvalue()) read

Context operation (above, open operation. Below, close operation)
with open() as variable name: no need to write close(), it will be executed automatically, the picture is a binary file, so add a b after opening the mode, such as' rb' is the binary reading mode.
Here, file.cloesd checks whether the file is closed by
context management. __enter__ and __exit__ (you don’t need to use with only to get and release resources. Use it to execute the content that it prints, exit. The content is always put in the last output)
Here is a hidden class that means open () to use enter and exit methods to open and close, and then print variables to operate (that is, with with will call the above two magic methods)
The with class can be instantiated and then accessed or used

File access encoding:
Generally, encoding=none is hidden behind the mode in brackets for accessing files, so there is no need to decode if there is no encoding.
1. For example, to write Chinese into printing, you need to write encoding='utf-8' in it. Garbled characters appear, 2. Write Chinese into the file, check the content of the file, and the input code will not appear garbled

os operating directories and files:
first import the module import os (the operation of importing the extension package)
print (os.getcwd()) to view the absolute path
os.listdir() returns all the contents of the directory into a list of elements, the absolute can be added in the brackets Path to locate accurately, but you need to add r
os.chdir() in front to change the current path, you don’t need to add print
mkdir, create a folder
rmdir, delete a folder (directory), you can only delete empty folders, you can’t delete files,
remove, delete File, cannot delete the folder
os.system(), automatically recognize the system and use it, call the system command, which system is recognized in which system terminal is used, enter ls in powershell to execute the linux subsystem, but not in cmd
os. rename (old name, new name)
os path module:
os.path.join (path, path) path splicing
os.path.basename() When printing, you need to pass in parameters, and return the short path (the last path) that is the folder where it is located

Guess you like

Origin blog.csdn.net/qwe863226687/article/details/113969083