Python OS module

Source: Internet
Author: User

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‘
    • Os.access (' Pathfile ', OS. W_OK) Verify file permission mode, Output True,false

    • Os.chmod (' Pathfile ', OS. W_OK) Changing the file permission mode
?
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.