Python Full Stack development "supplement" review some common operations of OS module

Source: Internet
Author: User

Import os# 1. Switch path =============d = OS.GETCWD () #获取当前的工作路径os. ChDir (' d:\\ ') #目录的切换print (OS.GETCWD ()) # (Toggle the past how to come back?) I'll be back in a chdir. Os.chdir (d) Print (OS.GETCWD ()) # 2. Execute the System commands =============# system and Popen are all execution systems commands, but popen is more useful, Because it has a return value of Os.system (' dir ') #显示的是gbk的编码, # solves the system garbled method ret = Os.popen (' dir ') #popen是有返回值的, and self-transcoded print (Ret.read ()) # 3. Create folder = and create file ==========os.mkdir (' temp ') #生成一个文件夹, you can only generate one Os.mkdir (R ' Temp2\inner ') #这样就报错了os. Makedirs (R ' Temp1\inner ' , Exist_ok=true) #创建多级目录os. Makedirs (R ' Temp1\inner\inner2 ', exist_ok=true) #创建多级目录 # So if the folder already exists, then the error, if I do not want to make an error ( If there is, do not create or error), then add exist_ok=true# create file F = open (R ' Temp1\inner\file ', ' W ') F.close () # 4.====== Rename folder =====os.rename ( R ' Temp1\inner\inner2 ', ' Temp1\inner\haiyan ') # 5.===== Delete the folder and delete the file =========# first deleting the file, Os.remove (R ' Temp1\inner\file ') # Then delete the folder Os.removedirs (R ' Temp1\inner\haiyan ') #删除一个文件夹的时候, if the previous level of the folder is empty, it is deleted. And so on Os.rmdir ((R ' Temp1\inner ')) #只删除一个文件夹 # 6. Subdirectory ========print (Os.listdir (OS.GETCWD ())) #打印当前目录下的目录print (Os.walk ( OS.GETCWD ()) #<generator Object waLK at 0x00000000021c6728>ret = Os.walk (OS.GETCWD ()) #拿到的东西比较多, if you care about something under a subdirectory, use Walkprint (list (ret)) # 7.==== Structure description for obtaining information about a file or directory ========print (os.stat (' temp ') # st_atime: Last Accessed Time # St_mtime: Last Modified Time # st_ctime: Latest update time print ( OS.SEP) # Prints print (OS.GETCWD ()) File_path = '%s%s%s '% (OS.GETCWD (), os.sep, ' filename ') #拼接一个路径 (mode one) print (File_path) Print (Os.path.join (OS.GETCWD (), ' filename ')) #拼接一个路径 (mode two) # 8.==== string indicates the current use of platform print (os.name) #如果是win, then the printed NT if yes, Linux, Print poxis# application scenario: When you enter a command, you have to decide whether it is a win or a Linux system. You can use # Os.name to judge the # 9.=== get the System environment variable =====print (os.environ) # 10. Path-related =======print (Os.path.abspath (' namedtuple.py ')) Print (Os.path.dirname (Os.path.abspath (' namedtuple.py '))) print (Os.path.dirname (Os.path.dirname (Os.path.abspath ( ' namedtuple.py '))) print (Os.path.exists (Os.path.abspath (' namedtuple.py ')))

  

Python Full Stack development "supplement" review some common operations of OS 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.