Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Misleading global variable
- import os
- import zipfile
- import ConfigParser
- import ftputil
- import shutil
- import tempfile
- import time
- def download_files():
- # Alvin: Think of a better variable name
- file_list = ftp.listdir(downloads)
- for filename in file_list:
- # Alvin should be ftp.path.join
- source = os.path.join(downloads, filename)
- target = os.path.join(temp_path, filename)
- ftp.download(source, target)
- def zipping_files():
- dirlist = os.listdir(temp_path)
- global filepath
- filepath = os.path.join(temp_path, 'part2b.zip')
- # Alvin: find better name for zip_name
- global zip_name
- zip_name = zipfile.ZipFile(filepath, 'w')
- # Alvin: try not to use built-in names as variable names
- for list in dirlist:
- # Alvin: file_path, absolute_path
- get_file = os.path.join(temp_path, list)
- zip_name.write(get_file, 'part2b\' + list)
- def upload_files():
- ftp.upload(filepath, uploads + '/part2b.zip')
- def main():
- # Alvin: Do not use globals.
- # Alvin: remove useless and above all misleading comments
- global temp_path
- temp_path = tempfile.mkdtemp()
- config = ConfigParser.ConfigParser()
- config.readfp(open('config.ini'))
- server = config.get('main', 'Server')
- username = config.get('main', 'Username')
- password = config.get('main', 'Password')
- global uploads
- uploads = config.get('main', 'Upload folder')
- global downloads
- downloads = config.get('main', 'Download folder')
- #connect to ftp
- global ftp
- # Alvin: open ftp connection when you only need it.
- ftp = ftputil.FTPHost(server, username, password)
- try:
- download_files()
- zipping_files()
- upload_files()
- finally:
- ftp.close()
- zip_name.close()
- shutil.rmtree(temp_path, 'true')
- print "Successfully transferred files."
- print ""
- print "This will close in 5 seconds. . . . ."
- time.sleep(5)
- if __name__ == '__main__':
- main()
- try:
- download_files(downloads, temp_path)
- zipping_files(temp_path)
- upload_files(ftp)
- def download_files(downloads, temp_path):
- def zipping_files(temp_path):
- def upload_files(ftp):
- import os
- import zipfile
- import ConfigParser
- import ftputil
- import shutil
- import tempfile
- import time
- def download_files(downloads, temp_path, server, username, password):
- ftp = ftputil.FTPHost(server, username, password)
- file_list = ftp.listdir(downloads)
- for filename in file_list:
- source = os.path.join(downloads, filename)
- target = os.path.join(temp_path, filename)
- ftp.download(source, target)
- ftp.close()
- def zipping_files(temp_path):
- file_list = os.listdir(temp_path)
- filepath = os.path.join(temp_path, 'part2b.zip')
- zip_file = zipfile.ZipFile(filepath, 'w')
- for filename in file_list:
- file_path = os.path.join(temp_path, filename)
- zip_file.write(file_path, 'part2b\' + filename)
- zip_file.close()
- def upload_files(uploads, temp_path, server, username, password):
- ftp = ftputil.FTPHost(server, username, password)
- filepath = os.path.join(temp_path, 'part2b.zip')
- ftp.upload(filepath, uploads + '/part2b.zip')
- ftp.close()
- def main():
- temp_path = tempfile.mkdtemp()
- 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')
- try:
- download_files(downloads, temp_path, server, username, password)
- zipping_files(temp_path)
- upload_files(uploads, temp_path, server, username, password)
- finally:
- shutil.rmtree(temp_path, 'true')
- print "Successfully transferred files."
- print ""
- print "This will close in 5 seconds. . . . ."
- time.sleep(5)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment