繁体   English   中英

如何将文件从文件夹移动到其子文件夹?

[英]How to move file from a folder to its sub folder?

我正在使用 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 移动到相应的子文件夹?

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

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

你可以:

  • 添加一个额外的elif来捕获它并且什么都不做。
  • 忽略它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM