Topic: FTP files ONLY files with specific extensions

Hi. New in python but managed to find my way around. I have folders with date names(i.e 20111011, or 20111101). My script goes and ftps the folder that matches the date passed(python CallTibcoWS.py 20111104). If the folder name matches the date passed then it will copy the folder & its contents to a specified location. Now I want to use the same code to copy files. File extensions are dates(i.e name.20111104, surname.20110102, python.20101111). I want to copy all the files that match the date passed when I run the script(python CallTibcoWS.py 20111104).....My code is as below. May you please correct the code below

import os
import datetime
import time
import sys
import subprocess
import paramiko
import platform

WSDESCRIPTOR = 'ESSImporter'
LOGFILE = 'C:/Logs/%s/%s_%s.log' % (WSDESCRIPTOR.lower(), WSDESCRIPTOR, datetime.datetime.now().strftime(

Thumbs up

Re: FTP files ONLY files with specific extensions

My code is as per below

import os
import datetime
import time
import sys
import subprocess
import paramiko
import platform

WSDESCRIPTOR = 'ESSImporter'
LOGFILE = 'C:/Logs/%s/%s_%s.log' % (WSDESCRIPTOR.lower(), WSDESCRIPTOR, datetime.datetime.now().strftime("%Y-%m-%d"))
TIBCOSERVER = 'GenevaServer'
TIBCOPORT = '7979'
WEBSERVICEADDRESS = "http://%(server)s:%(port)s/ESSImporter/Services/Interface/intfESSImporter-service.serviceagent/intfwsProcess_ESSImporterEndpoint1" % {'server': TIBCOSERVER, 'port': TIBCOPORT, 'ws': WSDESCRIPTOR}
FILEPATH='/export/home/extracts/out/%s'

REMOTEPATH='C:/ESSSWAP/Ess_Import/%s'

FTPSERVER = 'Server'
FTPUSER = 'test'
if platform.system().upper() == 'WINDOWS' : #dev environment
   FTPPASSWORD = "C:/Priv_Key.ppk"
else:
    FTPPASSWORD = "/export/Priv_Key.ppk"

#below code is wrong -- I tried a 2D array but since the date will be unknown the this will not work   
FILEEXT = 'date is the extension_%s' % (datetime.datetime.now().strftime("%Y-%m-%d"))
FILELIST = (('basket_div_SA.0', 'basket_div_SA'), ('basket_payment_SA.0', 'basket_payment_SA'), ('basket_summary_SA.0', 'basket_summary_SA'), ('basket_swap_SA.0', 'basket_swap_SA'), ('basket_trade_SA.0', 'basket_trade_SA'), ('interest_detail_SA.0', 'interest_detail_SA'), ('unwind_SA.0', 'unwind_SA'))
   

def getfile(ftphost, fromfile, tofile, tibcoinstanceInfo=None, tibcoinstanceError=None):
   
   
    if tibcoinstanceInfo:
        tibcoinstanceInfo('Connecting to %s as %s with pw %s' % (ftphost, FTPUSER, FTPPASSWORD))
       
    try:
        t = paramiko.Transport((ftphost, 22))
        key = paramiko.RSAKey.from_private_key_file(FTPPASSWORD)
        t.connect(username=FTPUSER, pkey=key)
        sftp = paramiko.SFTPClient.from_transport(t)
       
        #in this phase, I need to know which file will be copied so I suppose the code would go here
                     
        if sftp != None:
            sftp.get(fromfile, tofile)

            sftp.close()
       
            if tibcoinstanceInfo:
                tibcoinstanceInfo('FTP of the file was successful')
            result = True
        else:
            if tibcoinstanceError:
                tibcoinstanceError('Could not connect to the FTP server')
            result = False
       
       
    except Exception, e:
        if tibcoinstanceError:
            tibcoinstanceError('Error FTPing the file from %s to %s: %s: %s' % (fromfile,tofile,e.__class__, e))
        result = False
       
    return result

Thumbs up

Re: FTP files ONLY files with specific extensions

>>> s = 'abcd.101520'
>>> s.endswith(str(101520))
True
>>> s.endswith(str(101521))
False
>>>
import os.path
help(os.path)

look os.path.isfile()

Thumbs up