Python |在用户创建的文件夹中移动特定文件

Python | Moving specific Files in User created Folder

我写了一个简短的脚本,我想将所有 .CR2(在下一步中我想在前 2 个或 6 个文件之间进行选择)文件移动到一个文件夹,该文件夹之前已创建为 raw_input.

import os
from os import path
import shutil
import itertools

proname = raw_input("Please Name the Productfolder: ")

path = "/Volumes/01_Produktfotos/_2020-01-JANUAR/"

os.mkdir(proname)
os.chdir(proname)
os.makedirs('_final')
os.makedirs('_images')
os.makedirs('_psd')

sourcepath = '/Volumes/01_Produktfotos/_2020-01-JANUAR/03.01/'
sourcefiles = os.listdir(sourcepath)
destinationpath = '/Volumes/01_Produktfotos/_2020-01-JANUAR/03.01/%proname/_images/'
for file in sourcefiles:
    if file.endswith('.CR2'):
        shutil.move(os.path.join(sourcepath,file), os.path.join(destinationpath,file))

目前,脚本会创建用户特定的文件夹 (proname) 并在其中生成子文件夹 _images、_final 和 _psd。

我的问题是它不会从用户创建的文件夹中的顶级文件夹移动文件。

完美的结果是

  1. 我可以选择产品文件夹名称
  2. 它在文件夹内创建子文件夹 _images、_final 和 _psd
  3. 我可以选择是否要在创建的产品文件夹的子文件夹 _images 中放置前 2-6 个 .CR2 文件
  4. 脚本 运行 直到没有 .CR2 文件剩余

欢迎任何帮助或提示 (:

提前致谢

doc一样,dst是一个目录,不是文件。

shutil.move(src, dst) Recursively move a file or directory (src) to another location (dst). If the destination is an existing directory, then src is moved inside that directory. If the destination already exists but is not a directory, it may be overwritten depending on os.rename() semantics.

# Before:
shutil.move(os.path.join(sourcepath,file), os.path.join(destinationpath,file))

# After:
shutil.move(os.path.join(sourcepath,file), destinationpath))

会起作用。

以下更改解决了问题,移动特定 proname 文件夹中的 .CR2 文件:

destinationpath = os.path.join('/Volumes/01_Produktfotos/_2020-01-JANUAR/03.01/', proname, '_images')

现在我进入下一步,不是所有的 .CR2 文件都应该移动。仅前 2 或 6 个文件。