The OS module provides new/delete/view file properties for directories or files, as well as path operations for files and directories. For example: absolute path, parent directory ...
OS.SEP can replace the operating system-specific path delimiter. "\ \" under Windows, "/" under Linux
The OS.LINESEP string gives the line terminator used by the current platform. For example, Windows uses ' \ r \ n ', Linux uses ' \ n ' and Mac uses ' \ R '.
OS.PATHSEP output A string used to split the path of a file, using this character to split the search path (like path), such as POSIX on ': ', on Windows '; '
OS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work
Os.chdir ("dirname") changes the current script working directory, equivalent to the shell CD
Os.curdir returns the current directory: ('. ')
Os.pardir Gets the parent directory string name of the current directory: (' ... ')
Os.mkdir (' dirname ') generates a single-level directory, equivalent to mkdir dirname in the shell
Os.makedirs (' dirname1/dirname2 ') can generate multi-level recursive directories
Os.remove (file) Delete a file
Os.removedirs (' dirname1 ') if the directory is empty, then delete, and recursively to the previous level of the directory, if also empty, then delete, and so on
Os.rmdir (' dirname ') delete the single-level empty directory, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir dirname
Os.listdir (' dirname ') lists all files and subdirectories in the specified directory, including hidden files, and prints as a list
Os.rename ("Oldname", "newname") Rename file/directory if newname exists replace error
Os.replace (src,dest) Rename the file/directory, if the dest represents a file, there is overwrite the original file, no error, if the directory, there will be an error
Os.chmod (path, mode, *, Dir_fd=none, follow_symlinks=true)
Ex:os.chmod (' c:\\my_share_file\\test.sh ', 755)
Os.stat (' path/filename ') get File/directory information
Os.utime (path,times) Modify time attribute times is a tuple, (atime,mtime), and these two times can be obtained by os.stat
Os.walk (top[, topdown=true[, onerror=none[, Followlinks=false]])
1.top indicates the path of the directory tree that needs to be traversed
The default value of 2.topdown is "True", which means that the files under the directory tree are returned first, and then the subdirectories of the directory tree are traversed. When the value of Topdown is "False",
This means traversing the directory tree's subdirectories, returning the files under subdirectories, and finally returning the files under the root directory.
The default value of 3.onerror is "None", which means that errors resulting from file traversal are ignored. If not NULL, provide a custom function tip error message after you continue traversing or throwing an exception to abort the traversal
The function returns a tuple that has 3 elements that represent the currently traversed directory, the list of directories currently traversed, and the list of files for the currently traversed directory
Os.walk () Example:
>>> Import OS
>>> for Root, dirs, files in Os.walk ("Wd/chat", Topdown=false):
... for name in files:
.. print (Os.path.join (root, name)) #打印文件绝对路径
... for name in dirs:
.. print (Os.path.join (root, name)) #打印目录绝对路径
The Os.name string indicates the platform you are using. For example, for Windows, it's ' NT ', and for Linux/unix users, it's ' POSIX '.
Os.getenv () Gets an environment variable if none is returned
Os.putenv (key, value) sets an environment variable value
Os.environ[] Get the value of the environment variable is equivalent: os.environ[' home ' <->os.getenv (' home ')
The Os.system (command) function is used to run the shell command.
Os.popen ("Bash command") runs the shell command, generates the object, assigns it to the variable, and then reads it with read
The Python module OS