一、os.getcwd() os.chdir(dir) #獲取當前目錄 ,改變當前目錄爲dirwindows
二、os.listdir(dir) #獲取目錄內容,其結果爲list類型spa
三、os.mkdir(dir) #建立目錄pdo
四、os.rmdir(dir) # 刪除空目錄,若是目錄中有內容,則出錯rem
五、os.path.isdir(dir) #判斷是否爲目錄get
六、os.path.isfile(file) #判斷是否爲文件cmd
七、os.path.isabs(path) #判斷是否爲絕對路徑it
八、os.path.abspath(path) #取得絕對路徑擴展
九、os.path.dirname(path) #取得父目錄file
十、os.path.exists(path) #判斷目錄或文件是否存在遍歷
十一、os.path.getsize(path) #取得文件大小
十二、os.path.getctime(path) getmtime(path) getatime() #取得文件的建立、修改、最後存儲的時間
#不過取得是浮點數,須要用time模塊中的time.ctime(float) 或time.localtime(float)轉換成可識別的格式
1三、os.path.split(path) #分割路徑,結果爲元組,如(‘c:\\windows','system32')
1四、os.path.splitext(path) #分割擴展名,結果如 (‘c:\\windows\\system32\\cmd','.exe')
1五、os.rename(file1,file2) #將file1文件改名爲file2文件
1六、os.remove(file) #刪除file文件
1七、os.walk(path,topdown) #目錄遍歷
一下是調用os.walk的例子,遍歷指定的目錄
def walk_dir(dir, topdown=True):
for root, dirs, files in os.walk(dir, topdown):
for name in files:
print(os.path.join(root, name))
for name in dirs:
print(os.path.join(root, name))
glob模塊
glob.glob("*.py") #返回當前目錄下全部以.py爲後綴的目錄或文件
shutil模塊
shutil.copyfile(src,dst) #拷貝文件
shutil.copystat(src,dst) #拷貝文件,連同文件的stat一塊兒拷貝
shutil.copytree(src,dst) #拷貝目錄,拷貝以前dst必須不存在