python:从所有子目录收集具有一个扩展名的文件
我正在尝试收集所有子目录中的所有文件并移动到另一个目录 使用的代码python:从所有子目录收集具有一个扩展名的文件,python,directory,os.walk,collect,Python,Directory,Os.walk,Collect,我正在尝试收集所有子目录中的所有文件并移动到另一个目录 使用的代码 #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(
#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()
norshutil.move
norjoinpath()
对我有效。也许这是因为我试图更改元组中的元素,即os.walk
类似的代码适用于os.scandir
,但这只会收集当前目录中的文件
我怎样才能解决这个问题,谢谢
for root, dirs, files in os.walk(path, topdown=True, onerror=None, followlinks=True):
if root == empetrishki:
continue # skip the destination dir
for name in files:
basename, extension = os.path.splitext(name)
if extension.lower() == ".mp3":
oldpath = os.path.join(root, name)
newpath = os.path.join(empetrishki, name)
print(oldpath)
shutil.move(oldpath, newpath)
这就是我的建议。您的代码正在当前目录上运行,文件位于路径os.path.join(root,name)
,您需要为移动函数提供这样的路径
此外,我还建议使用os.path.splitext
来提取文件扩展名。更像蟒蛇。而且您可能希望跳过扫描目标目录
这就是我的建议。您的代码正在当前目录上运行,文件位于路径os.path.join(root,name)
,您需要为移动函数提供这样的路径
此外,我还建议使用os.path.splitext
来提取文件扩展名。更像蟒蛇。此外,您可能希望跳过扫描目标目录。如果您使用pathlib.Path(name),这并不意味着存在名为name的内容。因此,您确实需要注意是否有完整路径或相对路径,并且您需要确保解决这些问题。我特别要指出的是,您不会更改工作目录,并且有这样一行:
filePath = Path(name)
这意味着,当您沿着目录走的时候,您的工作目录可能不会改变。你应该从根和名字开始创建你的路径,这也是一个很好的解决方法,这样就可以知道完整的路径
filePath = Path(root).joinpath(name).resolve()
也可以将路径(根)放置在内部循环之外。现在您有了从“/home/”到文件名的绝对路径。因此,您应该能够使用.rename()重命名,如:
总而言之:
from pathlib import os, Path
empetrishki = Path.cwd().joinpath("empetrishki").resolve()
for root, dirs, files in os.walk(path, topdown=True, onerror=None, followlinks=True):
root = Path(root).resolve()
for name in files:
file = root.joinpath(name)
if file.suffix.lower() == ".mp3":
file.rename(empetrishki.joinpath(file.name))
如果您使用pathlib.Path(name),这并不意味着存在一个名为name的东西。因此,您确实需要注意是否有完整路径或相对路径,并且您需要确保解决这些问题。我特别要指出的是,您不会更改工作目录,并且有这样一行:
filePath = Path(name)
这意味着,当您沿着目录走的时候,您的工作目录可能不会改变。你应该从根和名字开始创建你的路径,这也是一个很好的解决方法,这样就可以知道完整的路径
filePath = Path(root).joinpath(name).resolve()
也可以将路径(根)放置在内部循环之外。现在您有了从“/home/”到文件名的绝对路径。因此,您应该能够使用.rename()重命名,如:
总而言之:
from pathlib import os, Path
empetrishki = Path.cwd().joinpath("empetrishki").resolve()
for root, dirs, files in os.walk(path, topdown=True, onerror=None, followlinks=True):
root = Path(root).resolve()
for name in files:
file = root.joinpath(name)
if file.suffix.lower() == ".mp3":
file.rename(empetrishki.joinpath(file.name))
我强烈建议您坚持使用pathlib模块,而不是os.path。谢谢您的帮助!我保存了
跳过dir
partI强烈建议坚持使用pathlib模块,而不是os.path。谢谢你也这么做了!我保存了跳过目录
部分谢谢你的解释!因为最后一行重命名/移动了它,所以它无法立即工作。我想这是因为给出了文件的.joinpath
完整PossixPath,而不仅仅是带有后缀的名称:file.rename(emperishki.joinpath(name))
啊,对了。我可以解决这个问题。您还可以执行file.name操作。:-)谢谢你的解释!因为最后一行重命名/移动了它,所以它无法立即工作。我想这是因为给出了文件的.joinpath
完整PossixPath,而不仅仅是带有后缀的名称:file.rename(emperishki.joinpath(name))
啊,对了。我可以解决这个问题。您还可以执行file.name操作。:-)