美文网首页
os模块详解

os模块详解

作者: 雀知安 | 来源:发表于2017-10-20 12:01 被阅读0次

import os

print os.path.dirname(file)获得当前文件的路径

输出C:/Users/TIME/Desktop/Demo_tornado

print os.path.join(os.path.dirname(file),'static')链接两个或多个路径名

输出C:/Users/TIME/Desktop/Demo_tornado\static

原型:os.path.join(path,*paths)
可以链接多个路径名:
如 :os.path.join(os.path.dirname(__file__),'static','picture')
输出:C:/Users/TIME/Desktop/Demo_tornado\static\picture

再如:os.path.join(os.path.dirname(__file__),'static','picture','dir1','dir2')
输出:C:/Users/TIME/Desktop/Demo_tornado\static\picture\dir1\dir2

注意*paths的顺序,链接路径名是根据前后顺序进行链接。
再如:os.path.join(os.path.dirname(__file__),'look','static','picture')
输出:C:/Users/TIME/Desktop/Demo_tornado\look\static\picture

os.path.exists(path)如果path是一个存在的文件,返回True。否则返回False。

如:print os.path.exists('C:\Users\TIME\Desktop/exists.png')
输出:True
print os.path.exists('C:\Users\TIME\Desktop/not.png')
输出:False

过续..

相关文章

网友评论

      本文标题:os模块详解

      本文链接:https://www.haomeiwen.com/subject/tqamuxtx.html