【发布时间】:2020-05-20 03:36:39
【问题描述】:
我正在尝试收集所有子目录的所有文件并移动到另一个目录
使用的代码
#collects all mp3 files from folders to a new folder
import os
from pathlib import Path
import shutil
#run once
path = os.getcwd()
os.mkdir("empetrishki")
empetrishki = path + "/empetrishki" #destination dir
print(path)
print(empetrishki)
#recursive collection
for root, dirs, files in os.walk(path, topdown=True, onerror=None, followlinks=True):
for name in files:
filePath = Path(name)
if filePath.suffix.lower() == ".mp3":
print(filePath)
os.path.join
filePath.rename(empetrishki.joinpath(filePath))
我在移动文件的最后一行遇到问题:filePath.rename() 或 shutil.move 或 joinpath() 都为我工作。也许那是因为我正在尝试更改元组中的元素 - os.walk 的输出
类似的代码适用于os.scandir,但这只会收集当前目录中的文件
如何解决这个问题,谢谢!
【问题讨论】:
标签: python directory os.walk collect