import os# gets the current directory print (OS.GETCWD ()) #改变当前目录os. ChDir ("D:\learn_python3") #生成单层目录os. mkdir ("1") #删除单层目录, The directory cannot have file Os.rmdir ("1") #生成多层目录os. Makedirs ("1\\test") #删除多层目录os. Removedirs ("1\\test") #列出指定目录下的所有文件和子目录, including hidden files, and print Os.listdir () #重命名文件/directory Os.rename ("One", "1") #获取文件/Catalog information print (Os.stat ("test.py")) #输出操作系统特定的路径分隔符print (os.sep ) #输出当前平台使用的行终止符print (OS.LINESEP) #输出用于分割文件路径的字符串print (os.pathsep) #输出字符串指示当前使用平台print (os.name) #运行shell命令, Direct display of the Os.system ("Free -m") #获取系统环境变量print (Os.environ) #返回path规范化的绝对路径print (Os.path.abspath ("D:\learn_python3\ Practice \ \ ")) #将path分割成目录和文件名二元组返回print (Os.path.split (" D:\learn_python3\ exercise \\test.py ")) #返回path的目录print (os.path.dirname ("D:\learn_python3\ Practice \\test.py")) #返回path最后的文件名print (Os.path.basename ("D:\learn_python3\ exercise \\test.py")) #如果path存在, returns True if path does not exist, returns Falseprint ( Os.path.exists ("D:\learn_python3\ Exercise")) #如果path是绝对路径, return to Trueprint (Os.path.isabs ("Learn_python3")) #如果path是一个存在的文件, Returns True. Otherwise returns Falseprint (Os.path.isfile ("D:\learn_python3\ exercise \\1.py")) #如果path是一个存在的目录, returns True. Otherwise, return Falseprint (OS.Path.isdir ("D:\learn_python3\ Exercise")) #将多个路径组合后返回, parameters before the first absolute path are ignored for print (Os.path.join ("d:\\", "learn_python\\", "practice") #返回path所指向的文件或者目录的最后存取时间, print in Timestamps (Os.path.getatime ("test.py")) #返回path所指向的文件或者目录的最后修改时间, print in timestamp form ( Os.path.getmtime ("test.py"))
This article is from the "Eight Miles" blog, so be sure to keep this source http://5921271.blog.51cto.com/5911271/1897311
Python Road 27-os Module