Details of os module in python
- 2020-05-17 05:42:31
- OfStack
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 OS - specific path delimiters. "\" under windows, "/" under Linux
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 the string used to split the file path, and the system USES this character to split the search path (like PATH), such as ':' on POSIX, 'Windows '; '
os.getcwd()
Gets the current working directory, the directory path where the current python script is working
os.chdir("dirname")
Change the current script working directory; shell is equivalent to cd
os.curdir
Return to current directory: ('.')
os.pardir
Gets the string name of the parent directory of the current directory :('.. ')
os.mkdir('dirname')
Generate single-level directory; shell is equivalent to mkdir dirname
os.makedirs ('dirname1/dirname2') generates multiple levels of recursive directories
os.remove(file)
Delete 1 file
os.removedirs('dirname1')
If the directory is empty, delete, and recurse to the previous level 1 directory, if it is also empty, delete, and so on
os.linesep
0
Delete single level empty directory, if the directory is not empty can not be deleted, error report; This is equivalent to rmdir dirname in shell
os.listdir('dirname')
Lists all files and subdirectories in the specified directory, including hidden files, and prints them as a list
os.rename("oldname","newname")
Rename file/directory, substitution error if newname exists
os.replace(src,dest)
Rename the file/directory, if dest represents a file, overwrite the original file, no error is reported; If the directory exists, an error will be reported
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 the time attribute times is a tuple,(atime,mtime), which can be obtained through os.stat
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
1.top represents the path of the directory tree to traverse
2. The default value of topdown is "True", which means that the file under the directory tree is returned first, and then the subdirectory of the directory tree is traversed. When the value of Topdown is "False",
It means to traverse the subdirectory of the directory tree, return the files in the subdirectory, and finally return the files in the root directory
3. The default value of onerror is "None", which indicates the error caused by ignoring the file traversal. If it is not empty, a custom function will be provided to continue the traversal after an error message or an exception will be thrown to suspend the traversal
This function returns a tuple with three elements representing the directory currently being traversed, the list of directories currently being traversed, and the list of files in the directory currently being traversed
os.walk()
For example:
>>> 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 to the file
... for name in dirs:
... print(os.path.join(root, name)) # Print the absolute path to the directory
os.name
The 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 1 environment variable if none is not returned
os.pathsep
0
Set the value of an environment variable
os.pathsep
1
Get the value of the environment variable: os.environ[' HOME']
<
-
>
os. getenv (' HOME ')
os.system(command)
The shell () function is used to run the shell command.
os.popen("bash command")
Run the shell command, generate an object, assign it to a variable, and then read it with read