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