Regexp not performing the same in FTP versus Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • IamIan

    #1

    Regexp not performing the same in FTP versus Python

    Hello all,

    I'm trying to use a regular expression in an FTP script to list
    certain files. When run in a standard FTP session the command:

    dir ????????.??[oOdDnNmM]*

    returns 48 files. When I use the following Python script it prints
    roughly 12 files (a subset of the 48), ending with 'None':


    import ftplib, traceback

    ftp = ftplib.FTP('hos t')
    ftp.login(user= 'user', passwd='pass')

    try:
    admFiles = ftp.dir('?????? ??.??[oOdDnNmM]*')
    print admFiles
    except:
    traceback.print _exc

    ftp.quit()


    Is my Python syntax off?

    Thank you.

  • IamIan

    #2
    Re: Regexp not performing the same in FTP versus Python

    It's strange but since more files have been added to this directory
    the regexp appears to be working correctly. Sorry to bother the list
    and thanks for your time.

    Ian

    Comment

    • Marc 'BlackJack' Rintsch

      #3
      Re: Regexp not performing the same in FTP versus Python

      In <1170978596.337 428.261110@h3g2 000cwc.googlegr oups.com>, IamIan wrote:
      Hello all,
      >
      I'm trying to use a regular expression in an FTP script to list
      certain files. When run in a standard FTP session the command:
      >
      dir ????????.??[oOdDnNmM]*
      >
      returns 48 files. When I use the following Python script it prints
      roughly 12 files (a subset of the 48), ending with 'None':
      >
      […]
      >
      Is my Python syntax off?
      Your `re` syntax is off. What you give FTP is a shell or glob pattern,
      not a regular expression for the `re` module.

      The `fnmatch` module has a function to translate a glob pattern to a `re`
      pattern:

      In [8]: fnmatch.transla te('????????.??[oOdDnNmM]*')
      Out[8]: '........\\...[oOdDnNmM].*$'

      Ciao,
      Marc 'BlackJack' Rintsch

      Comment

      Working...