Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. Using python to copy only files with a given extension

Using python to copy only files with a given extension

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
pythonsysadmintoolsregexworkspace
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    matjame
    wrote on last edited by
    #1

    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 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"

    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_
    
    A 1 Reply Last reply
    0
    • M matjame

      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 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"

      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_
      
      A Offline
      A Offline
      Addy Tas
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups