1. The OS module

Files and directories are processed

Directory: This is a folder

Properties:

Os.name: View the current system type;

If it’s Windows –> NT

If it is MAX –> POSIX

Os. environ: Returns the environment variables of the current system (all)

Function:

Os.environ. get() : Gets one of the specified environment variables

Path classification:

Absolute path: The path with the disk runroot directory is the absolute path

pyhton

Relative path: A path value relative to a file (reference); Features: writing without disk characters

Os.getcwd () : Returns the full path (absolute path) of the currently executing.py file

Os. listdir(PATH) : Displays all files and directories in the PATH path

Os.mkdir (path) : Create a single-level folder. This method can only create directories. If the directory already exists, the creation fails

Os. makedirs(path) : Create a multi-level folder (cascading new). This method can only create directories. If the directory already exists, the creation fails, and an error is sent

Os.rmdir (path) : Deletes a single-level folder. This method can only delete the page game directory. If the directory does not exist, the deletion fails

Os. removedirs(path) : Removes multiple layers of folders (cascading). This method can only remove directories. If the directory does not exist, the creation fails

Os.remove (path) : Delete the file, if the file does not exist, report an error! [Note] This method can only delete files, not directories

Os. rename(old, new) : You can rename a file or directory

Os.system () : Execute system instructions: CLS, ipconfig, www.sangpi.comdir…

Os.path. join(PATH, fileName) : The concatenation of data in PATH and fileName is called a new path and is returned as a string

Os.path. isdir(path) : determines whether the contents of the path corresponding to a directory, and if so, returns True; Otherwise, it returns False

Os.path. isfile(path) : determines whether the contents of the path corresponding to the file are a file, and if so, returns True; Otherwise, it returns False

Os.path.exists (path) : Determine whether the content of the path corresponding to exists, if so, return True; Otherwise, it returns False

Os.path.getSize (PATH) : The capacity of the corresponding path content is checked, and the number of bytes is returned

Os.path.basename (path) : Passing in a path value returns the last/last part of the path

Os.path.dirname (path): Passing in a path value returns the last/previous portion of the path

Os.path. split(path) : Passing in a path value puts the last/preceding part of the path into the first element of the primitive,

/ The next part goes into the second element of the primitive

Os.path.splitext (path) : Passing in a path value will take the last one of this path. In the first element of the primitive ancestor,

And the following into the second element of the primitive

1. Character Set (character encoding) :

ASCII:

US standard size, range: find it yourself

Unicode code:

Universal code, it can contain Chinese, but unfortunately, it is not universal.

Utf-8 code:

Most popular on computers, it contains content from previous Unicode and extends it,

It can also save Chinese

UTF-8 holds Chinese characters. Each Chinese character takes up to three bytes.

GBK code:

Our Chinese code watch, it also inherited the GB2312, mainly used in some Chinese systems

GBK saves Chinese characters, and 1 Chinese character takes up 2 bytes of memory.

2. Bytes and characters

Remember: Everything in a computer is in bytes (storage)

Characters: we use a text editor to open, can read (not garbled), is the character file

Byte: we use the text editor to open, can not read, is the byte file

Consider: Is the file at the end of.doc a character file?

Not;

3. File reading

There are prescribed steps:

1). Open the file

2). Operate data (read and write)

3). Close file **

Open the file:

Fp = open(path, encoding=,errors=’Ignore’)

Operation data:

Open mode:

‘r’ : denotes readonly character (readonly)

#’r+’ : Added a write function to the read-only function

‘W’ : means to write only characters (readWrite), the first write, if you do not close the file, continue to write, then the original content will be overwritten

#’w+’ : Appends a read function to the write-only function

‘A’ : Indicates additional content

#’a+’ : Added a read function while keeping appended content

‘rb’ : denotes read-only bytes (binary data)

‘wb’ : means write section only (binary data)

Encoding:

If the code set does not show the definition, the corresponding is: GBK

We can also define UTF-8 according to our own needs

Errors:

If you do not write, the default is an error (encoding/decoding is inconsistent).

We can also say ignore, so we won’t get any errors, but we’ll get some garbled code

Encoding & Decoding:

Encoding: program –> file

Decode: file –> program

Remember: if the encoding and decoding do not match, it will be garbled (in Chinese).

Read the data from the file:

Fp = open (path, ‘r’)

Content = fp.read() # Read all content

Content = fp.read(num) # Read the number of bytes in num

Content = fp.readline() # Read a line

Content = fp.readlines() # Reads all lines and returns them as a list

Print (content) # Print the content read

[Note] Read the file, if the file does not exist, simply say: FileNotFounderError

Write data to a file:

fw= open(path, ‘w’)

Fw. Write (str1) # Writes the contents of str1 variable to the file corresponding to path (overwrite)

[Note] Write the file, if the file does not exist, create the file first, then write the content; If it exists, overwrite the previous content

fw = open(path, ‘a’)

Fw. Write (str1) # Writes the contents of str1 variable to the file corresponding to path (append)

Close the file:

Fp.close () # Close file (release resource)