Python-os Module

Source: Internet
Author: User

Import OS
Import Sys

__file__ #只保存文件名 (does not contain absolute paths)
Os.path.abspath (__file__) #返回绝对路径 (file name + absolute path)
Base_dir = Os.path.dirname (Os.path.abspath (__file__)) #文件所在绝对路径 (file name not included)
Sys.path.append (BASE_DIR) #将文件路径加入到环境变量 (!!!!!! )

#综上:
Os.path.abspath () #绝对路径 + file name
Os.path.dirname () #绝对路径
OS.GETCWD () #pwd
Os.chdir ("path") #cd "path"
Os.chdir ("..") #cd Upper Level
Os.makedirs ("Dir1/dir2") #递归创建目录
Os.removedirs (' dir ') #递归删除 until the directory is not empty
Os.remove (' file ') #删除文件
Os.makedir (' dir ') #创建一个目录
Os.listdir ("path") #ls "path", which defaults to the current working path
Os.rename ("F1", "F2")
Os.stat ("File1") #file1的创建信息
Os.sep () #输出路径分隔符
Os.linesep () #返回系统换行符
Os.pathsep () #返回路径分割符
Os.system (' cmd ') #模拟终端执行命令
Os.path.split (' path ') #将path分割成绝对路径和文件名二元组返回
Os.path.dirname (' path ') #返回绝对路径
Os.path.base (' path ') #返回文件名
Os.path.exist (' path ') #判断路径是否存在
Os.path.isabs (' path ') #判断是否是绝对路径
Os.path.isfile (' path ') #判断是否是文件
Os.path.isdir (' path ') #判断是否是路径
Os.path.join (Apath,bpath,cpath,....) #路径拼接, the system's path delimiter is found by default
#PS: string concatenation try to use the join () method to avoid using ' + '
Os.path.getatime () #最后存取时间
Os.path.getmtime () #最后修改时间

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.