Python Base 7.4 os module

Source: Internet
Author: User

#/usr/bin/python#coding =utf8# @Time: 2017/11/11 3:15# @Auther: Liuzhenchuan# @File: OS module. PYimport os #1. Get the operating system type via OS Os.name = Windows is NT type, Os.name = Linux is a POSIX typeprint(os.name) #2. Executing system commandsexecuted under #在windows cmd. The character format for Windows is gkm. Commonly used is the UTF8Print(os.system (' ipconfig ')) #3. Get the results of system commands under Windows with the Os.popen () module, the Os.popen () module returns the file format,# So you can read the execution results of the system commands through File.read () and find the 192.168.16.1 by using the Find () commandcontent = Os.popen (' ipconfig '). Read () print content.find (' 192.168.16.1 ') # 4. Methods of the OS modulePrint dir(OS) #5. Os.listdir lists the files under the directory. '. ' point is the current directory. print os.listdir ('. ') )# 76OS.GETCWD () returns the current working directoryprint os.getcwd () print Os.listdir (OS.GETCWD ()) # 7. Toggle the current directory,print os.chdir (R ' E: ') print os.getcwd () #8 create ' abc ' in the current directory)os.mkdir (' abc ')# 9 Delete the current directory fileos.remove (' 1.log ')# 10 Print operating system delimiter, Linux system is \ n; Windows system is \ r \ n; Mac system is \ rprint os.linesep ()# 11. Determine if the directory exists in the current directory, and if not, createprint ' # # ' *5 + " to determine whether the directory exists ' *5 If not os.path.exists (' Test '): os.mkdir (' Test ') else: Print ' test is ok ' # 12. Stitching Directoryprint ' # # ' *5 + " stitching directory ' + ' # # ' *5 print os.path.join ('. ') ,' aaa ',' BB ', 'cc ') # 13. Get the top level directory print ' # # ' * 5 +  " obtains the previous level directory ' + ' # # ' *5 print os.path.dirname (R ' e:\ ape Lesson python script \modules ')

Python Base 7.4 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.