簡體   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