2

我需要创建一个程序,该程序将从 FTP 服务器下载文件并将其压缩并上传到 FTP 服务器中的上传文件夹。我的旧代码很好,但我的导师要求我应该将我的代码组织成逻辑函数,并且在组织我的代码之后,它没有运行。这是完美运行的旧代码:

import os
import upload
import download
import zipfile
import ConfigParser
import ftputil
import shutil
import time


def main():

    #create a folder Temp on drive D:\ for later use
    path = r'D:\Temp'
    os.makedirs(path)

    #parse all the  values at configuration file named config.ini
    config = ConfigParser.ConfigParser()
    config.readfp(open('config.ini'))
    server = config.get('main', 'Server')
    username = config.get('main', 'Username')
    password = config.get('main', 'Password')
    uploads = config.get('main', 'Upload folder')
    downloads = config.get('main', 'Download folder')

    #connect to ftp
    ftp = ftputil.FTPHost(server, username, password)

    dirlist = ftp.listdir(downloads)

    #download all files from download folder to the created temp folder
    for list in dirlist:
        ftp.chdir(downloads)
        target = os.path.join(path, list)
        ftp.download(list, target)

    #zipping files
    absolute_path = r'D:\Temp'
    dirlist = os.listdir(absolute_path)
    filepath = r'D:\Temp\part2b.zip'
    zip_name = zipfile.ZipFile(filepath, 'w')
    for list in dirlist:
        get_file = os.path.join(absolute_path, list)
        zip_name.write(get_file, 'part2b\\' + list)

    #upload the zipfile named project2.zip to upload folder at ftp
    ftp.chdir(uploads)
    ftp.upload(filepath, uploads + '/part2b.zip')
    ftp.close()

#close the zipfile to remove the temp folder
    zip_name.close()

    #message
    print "Successfully uploaded all files in the upload folder"
    print ""
    print "this will close in 5 seconds....."


#delete temp folder
    shutil.rmtree(path, 'true')
    time.sleep(5)


if __name__ == '__main__':
    main()

这是新的组织代码,但没有正常运行,但是当我尝试 python 命令行中的所有功能时,它很好。我不知道这里有什么问题:

import os
import zipfile
import ConfigParser
import ftputil
import shutil
import time
import tempfile



def dl_function(source_folder, target_folder):
    dirlist = ftp.listdir(source_folder)
    for list in dirlist:
        source = os.path.join(source_folder, list)
        target = os.path.join(target_folder, list)
        ftp.download(source, target)

def zip_function(temp_folder):
    dirlist = os.listdir(temp_folder)
    filepath = os.path.join(temp_folder, 'project2b.zip')
    zip_name = zipfile.ZipFile(filepath, 'w')
    for list in dirlist:
        get_file = os.path.join(temp_folder, list)
        zip_name.write(get_file, 'part2b\\' + list)

def upload_function(target_folder):
    ftp.upload(filepath, target_folder + '/part2b.zip')


def main():

    temp_folder = tempfile.mkdtemp()

    #parse all the  values at configuration file named config.ini
    config = ConfigParser.ConfigParser()
    config.read(open('config.ini'))
    server = config.get('main', 'Server')
    username = config.get('main', 'Username')
    password = config.get('main', 'Password')
    uploads = config.get('main', 'Upload folder')
    downloads = config.get('main', 'Download folder')

    ftp = ftputil.FTPHost(server, username, password)

    try:

        dl_function(downloads, temp_folder)
        zip_function(temp_folder)
        upload_function(uploads)

    finally:
        ftp.close() 
        zip_name.close()        

    print "This will close in 5 seconds"

    #delete temp folder
    shutil.rmtree(temp_folder, 'true')
    time.sleep(5)

if __name__ == '__main__':
    main()
4

2 回答 2

3

这是一个很好的例子,说明您应该如何在应用程序中使用日志记录。通常,当事情发生爆炸时,您可以通过两种方式对其进行调试

  • 附加调试器
  • 查看一些详细的日志记录

后者更适合部署的代码,因为您并不总是手边有调试器。

查看pydocs 页面上的日志记录信息。为您的代码添加:-

import os
import zipfile
import ConfigParser
import ftputil
import shutil
import time
import tempfile
import logging



def dl_function(source_folder, target_folder):
    logging.trace("Running dl_function")
    dirlist = ftp.listdir(source_folder)
    logging.trace("Got a dirlist")
    for list in dirlist:
        logging.trace("Running running for item " + list + " in dirlist")
        source = os.path.join(source_folder, list)
        logging.trace("Setting source to " + source)
        target = os.path.join(target_folder, list)
        logging.trace("Setting target to " + source)
        logging.trace("Attempting FTP download from " + source + " to " + target)
        ftp.download(source, target)
        logging.info("File downloaded from FTP")

...
...
...

if __name__ == '__main__':
    logging.info("Starting downloading application")
    main()

日志记录中最重要的部分是正确拆分日志记录,因此您使用正确的级别(DebugInfoWarningErrorCritical)。在 pydocs 页面上有一个教程。

通过查看日志的输出,您可以了解您的程序在执行过程中的进展程度以及它的死亡位置,并将其缩小到一两行。

于 2012-07-02T10:06:58.517 回答
1

upload_functiondl_function两者都尝试使用未定义的变量(ftpand filepathinupload_functionftpin dl_function)。

您在 finally 块中也有一个zip_name.close()调用,main它应该真正属于zip_function(这将需要它自己的 try/finally 块)。

wrt/ 错误消息,您应该从命令行窗口运行脚本以查看整个回溯和错误消息。

完全不相关,但您的命名可以改进(即“download_files”而不是“dl_function”)......

于 2012-07-02T10:30:24.893 回答