import os
獲得當前文件的路徑
1,os.getcwd()
輸出:'C:\\Python3'
2,print os.path.dirname(__file__) #py文件才有用,交互式?jīng)]有__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的順序,鏈接路徑名是根據(jù)前后順序進行鏈接狡蝶。
再如: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
刪除文件/夾,清空目錄
0缩幸,刪除文件:os.remove(path)
1集惋,文件夾空:os.removedirs(path)
刪除path目錄(連同里面的文件)
shutil.rmtree(TRAIN_DATA_DIR)
創(chuàng)建目錄
os.makedirs(TRAIN_DATA_DIR)
復制文件到目錄target_path
import shutil
shutil.copy(filename, target_path)