【问题标题】:Python: How to move list of folders (with subfolders) to a new directoryPython:如何将文件夹列表(带有子文件夹)移动到新目录
【发布时间】:2017-09-16 20:22:41
【问题描述】:

我有一个目录,实际上有 3000 多个文件夹(所有文件夹都有子文件夹)。

我需要做的是查看这些文件的目录并将它们移动到另一个文件夹。我做了一些研究,发现shutil 用于移动文件,但我不确定如何输入要查找的文件列表。

例如,在目录中,我想将以下文件夹(及其子文件夹)移动到另一个名为“Merge 1”的文件夹

1442735516927 1442209637226 1474723762231 1442735556057 1474723762187 1474723762286 1474723762255 1474723762426 1474723762379 1474723762805 1474723762781 1474723762936 1474723762911 1474723762072 1474723762163 1474723762112 1442209642695 1474723759389 1442735566966

我不确定从哪里开始,因此非常感谢任何帮助。谢谢!

【问题讨论】:

标签: python directory shutil


【解决方案1】:

结合 os 和 shutil,以下代码应该可以回答您的具体问题:

import shutil
import os

cur_dir = os.getcwd() # current dir path
L = ['1442735516927', '1442209637226', '1474723762231', '1442735556057',
        '1474723762187', '1474723762286', '1474723762255', '1474723762426',
        '1474723762379', '1474723762805', '1474723762781', '1474723762936',
        '1474723762911', '1474723762072', '1474723762163', '1474723762112',
       '1442209642695', '1474723759389', '1442735566966']

list_dir = os.listdir(cur_dir)
dest = os.path.join(cur_dir,'/path/leadingto/merge_1') 

for sub_dir in list_dir:
    if sub_dir in LL:
        dir_to_move = os.path.join(cur_dir, sub_dir)
        shutil.move(dir_to_move, dest)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    相关资源
    最近更新 更多