The OS module provides a unified operating system interface function that allows the OS module to automatically switch between specific functions in different operating system platforms, such as Nt,posix, for cross-platform operation.
- 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 to DirName
- Os.curdir returns the current directory: ('. ')
| 12345 |
import os os.getcwd () #输出 '/volumes/ Leopard/users/caroline ' os.chdir ( '/volumes ' ) os.getcwd () #输出 '/volumes ' os.curdir #输出 "." |
- Os.makedirs () can generate multi-level recursive directories
- Os.removedirs () can delete multiple levels of recursive empty directory, if the directory is not empty can not be deleted
- Os.mkdir () generate a single-level catalog
- Os.rmdir () Delete single-level empty directory, if the directory is not empty can not be deleted, error
- Os.pardir () Gets the parent directory string name of the current directory
- Os.listdir () lists all files and subdirectories under the specified directory, including hidden files
- Os.tmpfile () Create and open ' w+b ' a new temporary file
- Os.remove () Delete a file
- Os.rename ("Oldname", "newname") Rename file
- OS.SEP outputs the operating system-specific path delimiter. Win under "\ \", Macx "/"
- OS.LINESEP output The line terminator used by the current platform
- OS.PATHSEP output string for splitting the file path
- The Os.name output string indicates the current usage platform. Win-> ' NT '; Mac-> ' POSIX '
- Os.system (command) to run the shell command
?
| 1234 |
importosos.sep #输出"/"os.linesep #输出"\n"os.pathsep #输出":" |
- Os.environ Getting system environment variables
?
| 123 |
import osos.environ #输出{‘LANG‘: ‘en_US.UTF-8‘, ‘TERM‘: ‘xterm-color‘, ‘Apple_PubSub_Socket_Render‘: ‘/tmp/launch-cQd0wl/Render‘, ‘VERSIONER_PYTHON_PREFER_32_BIT‘: ‘no‘, ‘VERSIONER_PYTHON_VERSION‘: ‘2.6‘, ‘SHLVL‘: ‘1‘, ‘SSH_AUTH_SOCK‘: ‘/tmp/launch-Wd5ZJI/Listeners‘, ‘TERM_PROGRAM_VERSION‘: ‘273‘, ‘__CF_USER_TEXT_ENCODING‘: ‘0x1F5:0:0‘, ‘PWD‘: ‘/Volumes/Leopard/Users/Caroline‘, ‘SHELL‘: ‘/bin/bash‘, ‘LOGNAME‘: ‘Caroline‘, ‘USER‘: ‘Caroline‘, ‘HOME‘: ‘/Volumes/Leopard/Users/Caroline‘, ‘PATH‘: ‘/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin‘, ‘COMMAND_MODE‘: ‘unix2003‘, ‘_‘: ‘/usr/bin/python‘, ‘DISPLAY‘: ‘/tmp/launch-VIFSmk/org.x:0‘, ‘TMPDIR‘: ‘/var/folders/Iy/IyQx9l7EHD0y+Z77k20XCU+++TI/-Tmp-/‘, ‘TERM_PROGRAM‘: ‘Apple_Terminal‘}os.environ.get(‘LOGNAME‘) #输出‘Caroline‘ |
?
| 12345 |
import os os.access ( '/volumes/leopard/users/caroline/desktop/1.mp4 ' ,os. W_OK) #输出True os.chmod ( '/volumes/leopard/users/caroline/desktop/1.mp4 ' ,os. X_OK) #修改权限为X os.access ( '/volumes/leopard/users/caroline/desktop/1.mp4 ' ,os. W_OK) #输出False os.access ( '/volumes/leopard/users/caroline/desktop/1.mp4 ' ,os. X_OK) #输出True |
Python OS module