Advertisement
Dedzbn

Untitled

Nov 24th, 2020
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. from ftplib import FTP
  5. from os import path
  6. from os.path import expanduser, join
  7.  
  8.  
  9. ftp = FTP("ftp_address")
  10. ftp.login("ftp_login", "ftp_password")
  11. ftp.cwd("/ftp/dir")
  12.  
  13. home_path = '~/Desktop/'
  14.  
  15.  
  16. def check_file1():
  17.     """Check file '1.txt' at ftp"""
  18.     ftp_files = []
  19.     for name in ftp.nlst():
  20.         ftp_files.append(name)
  21.     for x in range(len(ftp_files)):
  22.         if '1.txt' in ftp_files[x]:
  23.          return 1
  24.     return 0    
  25.        
  26.  
  27. def check_file2():
  28.     """Check file '2.txt' at ftp"""
  29.     ftp_files = []
  30.     for name in ftp.nlst():
  31.         ftp_files.append(name)
  32.     for x in range(len(ftp_files)):
  33.         if '2.txt' in ftp_files[x]:
  34.          return 1
  35.     return 0
  36.  
  37. def create_files():
  38.     """Build txt files to copy ftp"""
  39.     if check_file1() == 1 and check_file2() == 0: #if file '1.txt' exsist and '2.txt' not
  40.         with open (path.join(expanduser(home_path), '2.txt'),'w') as f:
  41.          pass
  42.     if check_file1() == 1 and check_file2() == 1: #both files are created
  43.         for x in range(3,11):
  44.             with open (path.join(expanduser(home_path), str(x) + '.txt'),'w') as f:
  45.              pass
  46.  
  47.  
  48. def download_ftp():
  49.     if check_file1() == 1 and check_file2() == 0:
  50.         file = path.join(expanduser(home_path), '2.txt')
  51.         with open(file, 'rb') as text_file:
  52.             ftp.storlines('STOR 2.txt', text_file)
  53.     elif check_file1() == 1 and check_file2() == 1:
  54.         for x in range(3,11):
  55.           file = path.join(expanduser(home_path), str(x) + '.txt')
  56.           name = path.join(str(x) + '.txt')
  57.           with open(file, 'rb') as text_file:
  58.               ftp.storlines('STOR ' + name, text_file)
  59.  
  60.  
  61. check_file1()
  62. check_file2()
  63. create_files()
  64. download_ftp()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement