1、os.getcwd() os.chdir(dir) #獲取當前目錄 ,改變當前目錄爲dir
2、os.listdir(dir) #獲取目錄內容,其結果爲list類型
3、os.mkdir(dir) #創建目錄
4、os.rmdir(dir) # 刪除空目錄,如果目錄中有內容,則出錯
5、os.path.isdir(dir) #判斷是否爲目錄
6、os.path.isfile(file) #判斷是否爲文件
7、os.path.isabs(path) #判斷是否爲絕對路徑
8、os.path.abspath(path) #取得絕對路徑
9、os.path.dirname(path) #取得父目錄
10、os.path.exists(path) #判斷目錄或文件是否存在
11、os.path.getsize(path) #取得文件大小
12、os.path.getctime(path) getmtime(path) getatime() #取得文件的創建、修改、最後存儲的時間
#不過取得是浮點數,需要用time模塊中的time.ctime(float) 或time.localtime(float)轉換成可識別的格式
13、os.path.split(path) #分割路徑,結果爲元組,如(‘c:\\windows','system32')
14、os.path.splitext(path) #分割擴展名,結果如 (‘c:\\windows\\system32\\cmd','.exe')
15、os.rename(file1,file2) #將file1文件更名爲file2文件
16、os.remove(file) #刪除file文件
17、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必須不存在