Using python to copy only files with a given extension
-
Hi. New in python but managed to find my way around. I have a folder named date(i.e 20111011, or 20111101). The 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 to a specified location. Now I want to use the same code to copy files. I have another folder(folder name not important), with files. File extensions are dates(i.e name.20111111, 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
import os
import datetime
import time
import sys
import subprocess
import paramiko
import platformWSDESCRIPTOR = '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"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) print(fromfile) _if sftp != None: sftp.get(fromfile, tofile) sftp.close() if tibc_
-
Hi. New in python but managed to find my way around. I have a folder named date(i.e 20111011, or 20111101). The 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 to a specified location. Now I want to use the same code to copy files. I have another folder(folder name not important), with files. File extensions are dates(i.e name.20111111, 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
import os
import datetime
import time
import sys
import subprocess
import paramiko
import platformWSDESCRIPTOR = '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"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) print(fromfile) _if sftp != None: sftp.get(fromfile, tofile) sftp.close() if tibc_
Do you want to discuss the code (structure, style, guidlines) or do you have a question about functionality(or the lack of)? For the discussion please state what you'd like to discuss, for the question elaborate on what exactly is not working.
Cogito ergo sum