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

问题描述 投票:1回答:1

[我写了一个简短的脚本,我想将所有.CR2文件(在下一步中,我要在前两个文件或第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))

此刻,脚本创建了用户特定的文件夹(名称),并在其中生成了子文件夹_images,_final和_psd。

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

完美的结果是,如果

  1. 我可以选择产品文件夹名称
  2. 它在文件夹内创建子文件夹_images,_final和_psd
  3. 我可以选择是否要在创建的Productfolder的子文件夹_images内放入前两个2-6 .CR2文件
  4. 脚本正在运行,直到没有.CR2文件为止

欢迎任何帮助或提示(:

预先感谢

python shutil
1个回答
0
投票

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

shutil.move(src,dst)递归将文件或目录(src)移至另一个位置(dst)。如果目标是现有目录,则将src移动到该目录内。如果目标已经存在但不是目录,则可能会根据os.rename()的语义将其覆盖。

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

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

将起作用。

© www.soinside.com 2019 - 2024. All rights reserved.