The OS module of the Python module

Source: Internet
Author: User

OS Module

A module that the Python interpreter interacts with the operating system. Used to manipulate directories and files.

# get current working directory Print (OS.GETCWD ())

# Change Directory os.chdir ('C:')print(OS.GETCWD ())  #  C +
Os.chdir (Os.curdir)  # ' . ' Go to the current directory Os.chdir (os.pardir)  #  ': ' Go to the parent directory

 os.makedirs ( " a/b/c  " ) #   Create multilevel directory  Os.removedirs ( '  a/b/c    ") #   Os.mkdir ( " a  " ) #   Create a single-level directory  Os.rmdir ( '  a     ") #   Delete single-level directory, empty delete, otherwise error! 
Print (Os.listdir ())  # lists all files and subdirectories under the specified directory, including hidden files, and prints as a list
Os.remove (file)  #  Delete file os.rename ('a''b ')  # Rename the file, the folder is empty can also be renamed, not empty error os.renames ('test/a'test1/ b')  #  You can name both the folder and the following file

Os.stat ('path/file')  #  Get File/directory information

Os.sep  #  Output operating system-specific path delimiter win under "\ \", Linux under "/"os.linesep  #  outputs the line terminator used by the current platform, Win under "\t\n", Linux under "\ n"os.pathsep  #  output is used to split the file path of the string win under; Linux is:

Print (os.name)  # The output string indicates the current usage platform. Win-> ' NT '; Linux-> ' POSIX' os.system ('dir')  #  run shell command, direct display  Print(Os.popen ('dir'). Read ())  #  Run the shell command to get the result of the execution  Print(os.environ)  #  get system environment variables

Os.path.abspath ('Path')#gets the absolute path of pathOs.path.split ('Path')#A tuple that splits path into directories and file names returnsOs.path.dirname ('Path')#the first element of a tuple that is equivalent to Os.path.split (' path ') splitOs.path.basename ('Path')#equivalent to the second element of a os.path.split (' path ') split tupleOs.path.exists ('Path')#determines if path exists, exists return true, does not exist return FalesOs.path.isabs ('Path')#determine if path is an absolute pathOs.path.isfile ('Path')#determine if path is a fileOs.path.isdir ('Path')#determine if path is a directoryOs.path.join ('path1','path2','Path3')#combine multiple paths togetherOs.path.getsize ('Path')#Gets the size of the path, the size of the directory to calculate the size of the files below him .Os.path.getatime ('Path')#returns the last access time of the file or directory pointed to by path, in the form of a timestampOs.path.getmtime ('Path')#returns the last modified time of the file or directory pointed to by path, in the form of a timestamp
Os.path

  

The OS module of the Python 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.