Python module OS, pythonos
The OS module allows you to create, delete, and view file attributes for directories or files, as well as perform path operations on files and directories. For example: absolute path, parent directory ......
OS. sep can replace the path separator specified by the operating system. "\" In windows and "/" in Linux "/"
The OS. linesep string specifies the row Terminator used by the current platform. For example, in Windows, '\ r \ n' is used, in Linux,' \ n' is used, and in Mac, '\ R' is used '.
OS. pathsep outputs the string used to split the file PATH. The system uses this character to split the search PATH (such as PATH), for example, ':' on POSIX and ';' on Windows ';'
OS. getcwd () obtains the current working directory, that is, the directory path of the current python script.
OS. chdir ("dirname") changes the current script working directory, which is equivalent to the cd
OS. curdir :('.')
OS. pardir :('..')
OS. mkdir ('dirname') to generate a single-level Directory, which is equivalent to mkdir dirname in shell.
OS. makedirs ('dirname1/dirname2 ') can generate multi-layer recursive directories.
OS. remove (file) delete an object
OS. removedirs ('dirname1') if the directory is empty, delete it and recursively go to the upper-level directory. If the directory is empty, delete it.
OS. rmdir ('dirname') deletes a single-stage empty directory. If the directory is not empty, it cannot be deleted and an error is returned. This is equivalent to rmdir dirname in shell.
OS. listdir ('dirname') lists all files and subdirectories in a specified directory, including hidden files, and prints them in list mode.
OS. rename ("oldname", "newname") rename the file/directory. If newname exists, an error occurs.
OS. replace (src, dest) Rename the file/directory. If dest indicates a file, it overwrites the original file and does not report an error. If the directory exists, an error is returned.
OS. chmod (path, mode, *, dir_fd = None, follow_symlinks = True)
Ex: OS. chmod ('C: \ my_cmd_file \ test. Sh', 755)
OS. stat ('path/filename ') to get the file/directory information
OS. utime (path, times) The modified time attribute times is a tuples (atime, mtime), which can be obtained through OS. stat.
OS. walk (top [, topdown = True [, onerror = None [, followlinks = False])
1. top indicates the path of the directory tree to be traversed
2. The default value of topdown is "True", indicating that the files under the directory tree are returned first, and then when the subdirectory of the directory tree is traversed. The value of Topdown is "False,
Then, the system first traverses the subdirectories of the directory tree, returns the files under the subdirectories, and finally returns the files under the root directory.
3. the default value of onerror is "None", indicating to ignore Errors generated by file time. if it is not empty, a user-defined function is provided to indicate an error message and continue traversal or throw an exception to stop traversal.
This function returns a tuple with three elements representing the directory currently traversed, the directory list currently traversed, and the file list of the directory currently traversed.
Example of OS. walk:
>>> Import OS
>>> For root, dirs, files in OS. walk ("wd/chat", topdown = False ):
... For name in files:
... Print (OS. path. join (root, name) # print the absolute path of the file
... For name in dirs:
... Print (OS. path. join (root, name) # print the absolute directory path
The OS. name string indicates the platform you are using. For example, for Windows, it is 'nt ', and for Linux/Unix users, it is 'posix '.
OS. getenv () gets an environment variable. If none is not returned
OS. putenv (key, value) sets an environment variable value
OS. environ [] gets the value of the environment variable. The two values are equivalent: OS. environ ['home'] <-> OS. getenv ('home ')
The OS. system (command) function is used to run shell commands.
OS. popen ("bash command") run the shell command to generate an object, which can be assigned to a variable and read by read.