我需要创建一个程序,该程序将从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()

最佳答案

这是有关如何在应用程序中使用日志记录的绝佳示例。通常,当发生问题时,您可以通过两种方式对其进行调试

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

  • 后者更适合于已部署的代码,因为您并不总是方便使用调试器。

    在pydocs页面上查看Logging信息。为您的代码添加:-
    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)。有一个tutorial over at the pydocs page

    通过查看日志的输出,您可以看到程序在执行过程中走了多远,在哪里死了,并将其缩小到一行或两行。

    07-27 13:13