Python OS module

Source: Internet
Author: User

Import OS Print (OS.GETCWD ()) #查看当前路径 Print (Os.chdir (R'C:\Users\Administrator\Desktop\ new Folder (2)'))  # chdir switch path, equivalent to the shell under the CD, the original quotation marks inside the path will be error (should be two \ \), but the front added R can be Print (OS.GETCWD ())

Results:

C:\Users\Administrator\PycharmProjects\untitled\ Module
None
C:\Users\Administrator\Desktop\ new Folder (2)

Save Time (^_^) and give the usual OS module usage

OS.GETCWD () Gets the current working directory, that is, the directory path of the current Python script work os.chdir ("DirName") changes the current script working directory, equivalent to the shell Cdos.curdir return the current directory: ('. ') Os.pardir Gets the parent directory string name of the current directory: (' ... ') Os.makedirs (' dirname1/dirname2 ') can generate multi-level recursive directory A\b\c\d\e......os.removedirs (' dirname1 ') if the directory is empty, then delete, and recursively to the previous level of the directory, if also empty, then delete , and so on Os.mkdir (' dirname ') to generate a single-level directory, equivalent to the shell mkdir dirnameos.rmdir (' dirname ') delete a single-level empty directory, if the directory is not empty can not be deleted, error; equivalent to the shell rmdir  Dirnameos.listdir (' dirname ') lists all files and subdirectories under the specified directory, including hidden files, and prints the Os.remove () in a list to delete a file Os.rename ("Oldname", "NewName") Rename File/directory Os.stat (' path/filename ') get File/directory information OS.SEP output operating system-specific path delimiter, win under "\ \", Linux for "/" OS.LINESEP output the current platform using the line terminator, Win under "\ r \ n", Linux for "\ n" os.pathsep output the string used to split the file path Os.name output string indicates the current use of the platform. Win-> ' NT ';  Linux-> ' POSIX ' Os.system ("Bash command") runs a shell command that directly displays the Os.environ get system environment variable Os.path.abspath (PATH) Return path normalized absolute path Os.path.split (path) splits path into directory and file name two tuples return Os.path.dirname (path) to the directory where path is returned. In fact, the first element of Os.path.split (path) os.path.basename returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of the Os.path.split (path) OS.Path.exists (path) If path exists, returns true if path does not exist, returns Falseos.path.isabs (path) If path is an absolute path, returns Trueos.path.isfile (path) Returns true if path is a file that exists. Otherwise, return Falseos.path.isdir (path) True if path is a directory that exists.  Otherwise return Falseos.path.join (path1[, path2[, ...])  When multiple paths are combined, the parameters before the first absolute path are ignored Os.path.getatime (path) returns the last access time of the file or directory to which path is pointing os.path.getmtime (path) Returns the last modified time of the file or directory to which path is pointing

Print (Os.path.split (r ' d:\ user directory \ My Pictures \1.png ')) print (Os.path.dirname (r ' d:\ user directory \ My Pictures \1.png ') print (os.path.basename (r ' d:\ user directory \ My picture \1.png ')) The result is: (' d:\\ user directory \ \ My picture ', ' 1.png ') d:\ user directory \ My picture 1.png

  

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.