日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【python】os模块 递归删除文件夹所有文件 --笔记

發(fā)布時間:2023/12/20 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【python】os模块 递归删除文件夹所有文件 --笔记 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
''' os.environ 獲取系統(tǒng)的環(huán)境變量 os.name nt -- windows \r\n | posix --- Linux \nos.path:''' import osprint(os.environ) print(os.environ['OS'])print(os.path.abspath('t1/file01.py')) # 獲取絕對路徑 print(os.path.isabs('t1/file01.py')) # 判斷所給的路徑是否是一個絕對路徑 print(os.path.isfile('t1/file01.py')) # True 判斷是否是文件 print(os.path.isdir('t1/file01.py')) # False 判斷是否是文件夾 print(os.path.exists('t1/file02.py')) # False 判斷是否存在文件夾或者文件 print(os.path.join(r'c:\foo', 'a.txt')) print(os.path.split(r'c:\foo\a.txt'))path = r'C:\考試1\chen\yuan\post bar\a.png' print(os.path.split(path))print(os.path.getsize(r'C:\images\desk_background.jpg')) # 單位字節(jié) # 5*1024*1024 1Mprint(os.path.getatime(r'C:\images\desk_background.jpg')) # 訪問時間 print(os.path.getctime(r'C:\images\desk_background.jpg')) # 創(chuàng)建時間 windows print(os.path.getmtime(r'C:\images\desk_background.jpg')) # 修改時間# os.remove() # path = 't2' # if os.path.isdir(path): # files = os.listdir(path) # if len(files) == 0: # os.rmdir('t1') # 刪除文件夾 # else: # for file in files: # path1 = os.path.join(path, file) # os.remove(path1) # 刪除文件# 也可以遞歸刪除文件 # def delAll(path): # if os.path.isdir(path): # files = os.listdir(path) # ['a.doc', 'b.xls', 'c.ppt'] # # 遍歷并刪除文件 # for file in files: # p = os.path.join(path, file) # if os.path.isdir(p): # # 遞歸 # delAll(p) # else: # os.remove(p) # # 刪除文件夾 # os.rmdir(path) # else: # os.remove(path) # # # delAll('c:/foo') ''' os mkdir() rmdir() 空的文件夾 非空: OSError: [WinError 145] 目錄不是空的。: 'c:/考試1' 遞歸的方式 import shutil shutil.rmtree(r'C:\bank_system') 非空文件夾的刪除os.listdir(path) 查看path下的內(nèi)容,并以列表的形式返回 os.chdir('c:/考試1') 切換目錄 os.getcwd() 獲取當前文件的路徑 (絕對路徑)os.getpid() get process id 獲取當前的進程id os.getppid() get parent process id 獲取父進程id''' import osprint(os.name) # try: # # os.mkdir('t2') # os.mkdir('c:/foo') # except FileExistsError: # print('文件夾已經(jīng)存在') # os.rmdir('c:/foo') # os.rmdir('c:/考試1')# import shutil # # shutil.rmtree(r'C:\bank_system')files = os.listdir(r'C:\online-store') print(files)print(os.getpid()) print(os.getppid())print(os.getcwd()) os.chdir('c:/考試1') # change dir print(os.getcwd()) list1 = os.listdir(os.getcwd()) print(list1)# 也可以遞歸刪除文件 def delAll(path):if os.path.isdir(path):files = os.listdir(path) # ['a.doc', 'b.xls', 'c.ppt']# 遍歷并刪除文件for file in files:p = os.path.join(path, file)if os.path.isdir(p):# 遞歸delAll(p)else:os.remove(p)# 刪除文件夾os.rmdir(path)else:os.remove(path)

?

總結(jié)

以上是生活随笔為你收集整理的【python】os模块 递归删除文件夹所有文件 --笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。