繁体   English   中英

将名称为字符串的 Zip 文件从一个目录复制到另一个目录

[英]Copy Zip files with string in name from one directory to another

我正在尝试构建一个脚本,将压缩文件从一个目录复制到另一个目录。 目前我有以下代码,它可以复制带有所需字符串_NLS压缩文件,但不能复制压缩文件。

import shutil
import os

source = r"C:\\directoryA\\"
dest = r"C:\\directoryB\\"

for file in os.listdir(source):
    if '_NLS' in file.lower():

        if not os.path.exists(dest):
            os.makedirs(dest)

shutil.copyfile(source + file, dest + file)

然后移动解压缩的文件而不是压缩的文件。

谢谢你的帮助,

卢克

主要的错误是根据 file.lower() 检查“_NLS”。 它总是返回小写字母,因此是假的。

第一次复制成功的原因是复制函数没有在循环中调用,因此只对列表中的最后一个文件调用一次。 请尝试以下操作:

import shutil
import os

source = r"source"
dest = r"dest"

if not os.path.exists(dest):
    os.makedirs(dest)

for file in os.listdir(source):
    if "_NLS" in file:
        shutil.copyfile(os.path.join(source, file), os.path.join(dest, file))

还可以使用os.path.join加入您的文件路径,以避免特定于操作系统的错误。

暂无
暂无

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

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