Python OS和shutil模塊的常見方法

1.列出指定目錄下所有的文件

os.listdir()

2.路徑拼接

os.path.join()

3.刪除文件

os.remove()

刪除文件夾

 import shutil
 shutil.rmtree(path)

4.重命名文件

os.rename()

5.取得當前工作目錄

os.getcwd()

os.path.dirname(__file__)

6.判斷文件夾/文件是否存在

os.path.exists()

7.判斷是否爲目錄

os.path.isdir()

8.判斷是否爲文件

os.path.isfile()

9.獲取文件大小 (單位是字節)

os.path.getsize()

10.創建目錄/多層目錄

os.mkdir()/makedirs()

11.是否爲絕對路徑

os.path.isabs()

12.移動文件

shutil.move(srcfile,dstfile)

13.複製文件

shutil.copyfile(srcfile,dstfile)

14.獲取文件的上級目錄

os.path.dirname()

15.獲取絕對路徑

os.path.abspath()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章