Regexp not performing the same in FTP versus Python

I

IamIan

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('host')
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.
 
I

IamIan

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
 
M

Marc 'BlackJack' Rintsch

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.translate('????????.??[oOdDnNmM]*')
Out[8]: '........\\...[oOdDnNmM].*$'

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top