from pathlib import Path
import shutil
p = Path('aa','bb','cc','dd')
p_top = Path(p.parts[0])if p_top.exists():
shutil.rmtree(p_top)
p.mkdir(parents=True, exist_ok=True)
返回指定文件夹下的对象路径
from pathlib import Path
p = Path('c:/')# p = Path('c:') 也行
p = p /'a'/'b'# p = Path('c:/', 'a', 'b') 也可以 或者 p = Path(p, 'a', 'b')for i in p.iterdir():print(i)
遍历文件夹下所有的doc或docx文件
from pathlib import Path
defget_files(extensions):
all_files =[]for ext in extensions:
all_files.extend(Path('.').glob(ext))return all_files
files = get_files(('*.doc','*.docx'))