python 删除文件

2021-09-08  本文已影响0人  hehehehe
In [1]: from pathlib import Path
In [1]: root = Path('.') 
In [1]: for i in list(root.rglob("back")): 
   ...:     print(i) 
   ...:     shutil.rmtree(i) 
import os
import shutil

os.remove(path)   #删除文件
os.removedirs(path)   #删除空文件夹

shutil.rmtree(path)    #递归删除文件夹

from pathlib import Path
import os
import shutil

path = Path('/Users/xx/Downloads/main_prd/level2')
for iter in path.iterdir():
    if iter.is_dir():
        for iter2 in Path(iter).joinpath('road').glob("*.*"):
            if iter2.is_file() and iter2.name.startswith("R_LZone"):
                # shutil.rmtree(str(iter2))
                os.remove(str(iter2))
                # print(iter2)

上一篇下一篇

猜你喜欢

热点阅读