Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- from ftplib import FTP
- from os import path
- from os.path import expanduser, join
- ftp = FTP("ftp_address")
- ftp.login("ftp_login", "ftp_password")
- ftp.cwd("/ftp/dir")
- home_path = '~/Desktop/'
- def check_file1():
- """Check file '1.txt' at ftp"""
- ftp_files = []
- for name in ftp.nlst():
- ftp_files.append(name)
- for x in range(len(ftp_files)):
- if '1.txt' in ftp_files[x]:
- return 1
- return 0
- def check_file2():
- """Check file '2.txt' at ftp"""
- ftp_files = []
- for name in ftp.nlst():
- ftp_files.append(name)
- for x in range(len(ftp_files)):
- if '2.txt' in ftp_files[x]:
- return 1
- return 0
- def create_files():
- """Build txt files to copy ftp"""
- if check_file1() == 1 and check_file2() == 0: #if file '1.txt' exsist and '2.txt' not
- with open (path.join(expanduser(home_path), '2.txt'),'w') as f:
- pass
- if check_file1() == 1 and check_file2() == 1: #both files are created
- for x in range(3,11):
- with open (path.join(expanduser(home_path), str(x) + '.txt'),'w') as f:
- pass
- def download_ftp():
- if check_file1() == 1 and check_file2() == 0:
- file = path.join(expanduser(home_path), '2.txt')
- with open(file, 'rb') as text_file:
- ftp.storlines('STOR 2.txt', text_file)
- elif check_file1() == 1 and check_file2() == 1:
- for x in range(3,11):
- file = path.join(expanduser(home_path), str(x) + '.txt')
- name = path.join(str(x) + '.txt')
- with open(file, 'rb') as text_file:
- ftp.storlines('STOR ' + name, text_file)
- check_file1()
- check_file2()
- create_files()
- download_ftp()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement