Python module--os module, sys module

Source: Internet
Author: User
Tags python script

One, OS module

 1 os.getcwd () Gets the current working directory, that is, the directory path of the current Python script work 2 3 os.chdir ("dirname") changes the working directory of the current script; equivalent to the Shell CD 4 5 Os.curdir returns the current directory: ('. ') ) 6 7 Os.pardir Gets the current directory string name: (' ... ')                       8 9 os.makedirs (' dirname1/dirname2 ') can generate a multi-level recursive directory of Os.removedirs (' dirname1 ') if the directory is empty, then delete, and recursively to the previous level of the directory, if also empty, then delete, 12   And so on Os.mkdir (' dirname ') to generate a stand-alone directory, equivalent to shell species mkdir dirname15 os.rmdir (' dirname ')   Delete single-level empty directory, if the directory is not empty, you can not delete, error; equivalent to rmdir dirname os.listdir (' dirname ') Lists all files and sub-file directories under the specified directory, including hidden files, and prints as a list os.remove () 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 "\ n" os.linesep output the string used to split the file path win ";", Li Nux ":" Os.name output string knowledge is currently used by the platform. Win-and ' NT '; Linux--' POSIX ' Os.system ("Bash command") runs a shell command that directly shows the Os.environ get system environment variable Os.path.abspath (PATH) return Path normalized absolute path Os.path.split (path) divides path into directory and file name two tuples returnedOs.path.dirname (path) returns the directory of path. In fact, the first element of Os.path.split (path), Os.path.basename, returns the last file name of path. How path ends with/or \ will return a null value, which is the second element of Os.path.split (path) os.path.exists (PA TH) If path exists, returns true if path does not exist, returns FALSE47 Os.path.isfile (path) If path is an existing file, returns T into Russia, otherwise returns FALSE49 Os.path.isabs ( Path) If it is an absolute path, return True51 os.path.isdir (path) If path is an existing directory, return true, otherwise return False53 Os.path.join (path1[,path2[,...... ]]) After combining multiple paths, the first absolute path parameter 55 will be ignored when the Os.path.getatime (path) returns the last access to the file or directory that the path only wants Inter-os.path.getmtime (path) returns the last modified time of the file or directory pointed to

1 ImportOS2 #print (OS.GETCWD ()) #获取该脚本当前的目录路径3 #Os.chdir ('.. ') #去当前目录的上一级目录路径4 #print (OS.GETCWD ())5 6 7 Print(Os.path.split (R"c:/python36 Exercises/day22/day22.py"))#cut the path to the file name and print in a tuple form8 Print(Os.path.dirname (R"c:/python36 Exercises/day22/day22.py"))#returns the folder path where the file resides9 Print(Os.path.basename (R"c:/python36 Exercises/day22/day22.py"))#returns the file nameTenA ='c:/python36 Exercises' Oneb ='day22/day22.py' A Print(Os.path.join (A, B))#Path Stitching
View Code

Second, sys module

1 sys.argv    command line argument list, the first element is the program itself path 2sys.exit (n)  exit the program, exit normally exits (0)3 sys.version   Gets the version information of the Python interpreter 4 sys.maxint   the largest int value 5Sys.path    Returns the search path for the module, initialized with the value of the PYTHONPATH environment variable 6sys.platform  Returns the                      operating system platform name 7                                   
1 ImportSYS2 Print(SYS.ARGV)3 4Command=sys.argv[1]5Path=sys.argv[2]6 7 ifcommand=="Post":8     Pass9 Ten elifcommand=="Get": One     Pass A  - Import Time -  forIinchRange (100): theSys.stdout.write ("#") -Time.sleep (0.1) -Sys.stdout.flush ()
View Code

Python module--os module, sys module

Related Article

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.