【问题标题】:How to move file from a folder to its sub folder?如何将文件从文件夹移动到其子文件夹?
【发布时间】:2022-12-10 18:03:36
【问题描述】:

我正在使用 Python,并且必须将文件从一个文件夹移动到它的子文件夹。我尝试使用 shutil.move(),但它给出了一条错误消息(“无法将目录‘%s’移入自身”)

这是代码:

for file in your_files:
    if file in images:
        shutil.move(your_folder, images_folder)
    elif file in docs:
        shutil.move(your_folder, docs_folder)
    elif file in texts:
        shutil.move(your_folder, texts_folder)
    else:
        shutil.move(your_folder, others_folder)

images_folder、docs_folder、texts_folder 和 others_folder 都是 your_folder 的子文件夹。所以我的问题是,如何将文件从 your_folder 移动到相应的子文件夹?

【问题讨论】:

    标签: python file shutil file-move


    【解决方案1】:

    一切皆文件:目录即文件。目标目录是源文件夹中的一个文件。

    您正在尝试将目标文件夹自行移动到其中。

    你可以:

    • 添加一个额外的elif,捕捉它而不做任何事情。
    • 忽略它。

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      • 2018-10-16
      • 2015-05-06
      • 1970-01-01
      • 2022-10-25
      相关资源
      最近更新 更多