【问题标题】:Python - Watch a folder for new .zip file and upload via FTPPython - 监视新 .zip 文件的文件夹并通过 FTP 上传
【发布时间】:2015-06-25 02:48:35
【问题描述】:

我正在创建一个脚本来监视文件夹,获取任何新的 .zip 文件,然后通过 FTP 将它们上传到预定区域。由于尚未创建环境,目前正在本地执行 FTP 测试。

我采取的策略是首先解压缩到本地文件夹。然后,对本地文件夹中的文件执行 ftplib.storbinary 到 ftpdestination。但是,解压缩过程似乎正在运行,但我收到“文件不存在”错误,尽管我可以在文件夹本身中看到它。

另外,有没有办法直接解压到 FTP 位置?我一直无法找到一种方法,因此我采用了这种方法。

谢谢,从代码中删除了本地 ftp 信息。此代码中相关的所有路径都将被更改,最有可能是动态方式,但目前这是一个本地环境

extractZip2.py

​​>
import zipfile
import ftplib
import os
import logging
import time

from socket import error as socket_error


#Logging Setup
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('__name__')

FTPaddress = ''
FTPusername = ''
FTPpassword = ''
ftp_destination_location  = ''

path_to_watch = "C:/Users/206420055/Desktop/test2/"
before = dict ([(f,None) for f in os.listdir(path_to_watch)])

temp_destination_location = "C:/Users/206420055/Desktop/temp/"


def unzip(fullPath,temporaryPath):
    with zipfile.ZipFile(fullPath, "r") as z :
        logger.info("Unzipping {0}".format(fullPath))
        z.extractall(temporaryPath)
        logger.info("Unzipped into local directory     {0}".format(temp_destination_location))


def check_or_create_ftp(session, folder):
    """
    Checks to see if necessary folder for currenttab is available. 
    Creates the folder if not found, and enters it.
    """
    if folder not in session.nlst():
        logger.info('Directory for {0} does not exist, creating directory\n'.format(folder))
        session.mkd(folder)

    session.cwd(folder)

def check_or_create(temp_destination):
    """
    Checks to see if local savepath exists. Will create savepath if not exists.
"""
    if not os.path.exists(temp_destination):
        logger.info('Directory for %s does not exist, creating directory\n' % temp_destination)
        os.makedirs(str(temp_destination))


def transfer(address,username,password,filename,destination):
    logger.info("Creating Session")
    try:
        session = session_init(address,username,password,destination)
    except (socket_error,ftplib.error_perm) as e:
        logger.error(str(e))
        logger.error("Error in Session Init")
    else:
        try:
            logger.info("Sending File {0}".format(filename))
            send_file(session,filename)
        except (IOError, OSError, ftplib.error_perm) as e:
            logger.error(e)

def session_init(address,username,password,path):
    session = ftplib.FTP(address,username,password)
    check_or_create_ftp(session,path)
    logger.info("Session Established")
    return session

def send_file(session,filename):
    file = open(filename,'rb')
    logger.info('Sending File : STOR '+filename)
    session.storbinary('STOR '+ filename, file)
    file.close()

def delete_local_files(savepath, file):
    logger.info("Cleaning Up Folder {0}".format(savepath))
    os.remove(file)


while 1:
    time.sleep(5)
    after = dict ([(f,None) for f in os.listdir(path_to_watch)])
    added = [f for f in after if not f in before]
    removed = [f for f in before if not f in after]
    if added: print "Added: ",", ".join(added)
    before = after

    check_or_create(temp_destination_location)
    if added : 
        for file in added:
            print file
            if file.endswith('.zip'):
                unzip(path_to_watch+file, temp_destination_location)
                temp_files = os.listdir(temp_destination_location)
                print("Temp Files {0}".format(temp_files))
                for tf in temp_files:
                    print("TF {0}".format(tf))
                    transfer(FTPaddress,FTPusername,FTPpassword,tf,ftp_destination_location)
                #delete_local_files(temp_destination_location,tf)
        else:
            pass

编辑:添加错误图像

如上所示,我们在 temp 文件夹中看到了该文件。但是控制台显然显示了错误。

【问题讨论】:

    标签: python ftp ftplib


    【解决方案1】:

    改成

    from glob import glob
    zips_in_path = dict ([(f,None) for f in glob("{base_path}/*.zip".format(base_path = path_to_watch)])
    

    os.listdir 不包括路径的path_to_watch 部分,它只是文件名,但是 glob 包含。

    所以你也可以这样做

    after = dict ([(os.path.join(path_to_watch,f),None) for f in os.listdir(path_to_watch)])
    

    使用这两种方法中的任何一种,您都应该能够获得路径中文件的完整路径

    【讨论】:

    • 这很可能是一个不断增长的文件夹,这会不断解压缩文件夹中的所有文件吗?编辑:使用 after = 行,它会导致触发而没有放入“新”文件,并导致“没有这样的文件或目录:'C:/Users/206420055/Desktop/test2/C:/Users/206420055 /Desktop/test2/test20140518152930.zip'
    • 您没有解压缩其中任何一个...这只是为您提供文件夹中的文件列表(第一个仅列出 zip 文件...第二个列出所有文件)跨度>
    • 打印列表... 99% 的情况下,当您遇到问题时,您可以在终端上打印一些废话,然后立即查看您做错了什么...只需打印它们并查看你在做什么......
    • 文件路径问题,哎呀!
    猜你喜欢
    • 2017-03-23
    • 2012-02-22
    • 2013-09-26
    • 1970-01-01
    • 2012-10-27
    • 2012-09-18
    • 2013-04-30
    相关资源
    最近更新 更多