获取多个子文件夹中的所有.mp3文件并移动到PC上的一个文件夹的python脚本?

时间:2017-12-02 11:12:09

标签: python pc

有没有人有一个流畅的python脚本来收集和移动具有特定扩展名的文件从多个子文件夹到PC上的一个文件夹?

3 个答案:

答案 0 :(得分:0)

而不是使用glob:

首先找到所有以.mp3结尾的文件:

import os
filepath = []
for path, _, files in os.walk('./'):
    #You might want to exclude some directories or limit search files found to directories:
    #exclude paths that have 'Program Files' folder and only include all paths that have 'Downloads',
    #ie derp/Downloads/a/ or Downloads/durp/ included, anything with 'Program Files' excluded
    if ('Program Files' not in path) and ('Downloads' in path):
        #in case of windows system replace \ with /
        filepath.extend([os.path.join(path, file).replace('\\', '/') for file in files
                         if str(file).endswith('.mp3')])

现在您已将具有完整文件路径的文件列表移动到新位置:

for file in filepath:
    os.rename(filepath, 'new/folder' + os.path.split(filpath)[1])

答案 1 :(得分:0)

怎么样

from dkfileutils.path import Path
[mp3.move('/destination/dir') for mp3 in Path('/source/dir').glob('**/*.mp3')]

(完全披露:我是dkfileutils的作者)

答案 2 :(得分:-1)

os.chdir("D:\DOWNLOAD")                  # Change file direction first
for file in glob.glob("*.mp3"):      # search for the *.mp3 extension 
    time.sleep(1)
    shutil.move("path/to/current/file", "path/to/new/destination/for/file")

类似这样的东西,你必须通过os函数添加walk以获得所有子方向,