shutil ignore fails on passing a tuple?

Discussion in 'Python' started by Alex van der Spek, Jul 19, 2012.

  1. This beats me:
    ++++++++++++++++++++
    >>> ipatterns

    ('*.txt', '*.hdf', '*.pdf', '*.png')
    >>> igf = shutil.ignore_patterns(ipatterns)
    >>> ignorethis = igf(ddftopdir,os.listdir(ddftopdir))


    Traceback (most recent call last):
    File "<pyshell#60>", line 1, in <module>
    ignorethis = igf(ddftopdir,os.listdir(ddftopdir))
    File "C:\Python27\lib\shutil.py", line 138, in _ignore_patterns
    ignored_names.extend(fnmatch.filter(names, pattern))
    File "C:\Python27\lib\fnmatch.py", line 49, in filter
    pat=os.path.normcase(pat)
    File "C:\Python27\lib\ntpath.py", line 46, in normcase
    return s.replace("/", "\\").lower()
    AttributeError: 'tuple' object has no attribute 'replace'

    >>> igg = shutil.ignore_patterns('*.txt', '*.hdf', '*.pdf', '*.png')
    >>> ignorethat = igg(ddftopdir, os.listdir(ddftopdir))
    >>> ignorethat

    set(['Chi2.png', 'DTSdata.hdf', 'TST.hdf', 'BullNoseDiffs.png',
    'DTSall.hdf', 'Symmetry.pdf'])
    >>>

    ++++++++++++++++++++++++++++
    Why does it fail on passing in a tuple of ignore strings? I thought the ,
    (comma) is pretty much the tuple constructor (if that is the right word).

    How can I solve this? Is there a way to convert a tuple of strings in a form
    that will be accepted?

    Thank you in advance,
    Alex van der Spek
     
    Alex van der Spek, Jul 19, 2012
    #1
    1. Advertising

  2. > >>> ipatterns

    > ('*.txt', '*.hdf', '*.pdf', '*.png')

    > >>> igf = shutil.ignore_patterns(ipatterns)
    > >>> ignorethis = igf(ddftopdir,os.listdir(ddftopdir))

    >
    > Traceback (most recentcall last):
    > File "<pyshell#60>", line 1, in <module>
    > ignorethis = igf(ddftopdir,os.listdir(ddftopdir))
    > File "C:\Python27\lib\shutil.py", line 138, in _ignore_patterns
    > ignored_names.extend(fnmatch.filter(names, pattern))
    > File "C:\Python27\lib\fnmatch.py", line 49, in filter
    > pat=os.path.normcase(pat)
    > File "C:\Python27\lib\ntpath.py", line 46, in normcase
    > return s.replace("/", "\\").lower()
    > AttributeError: 'tuple' object has no attribute 'replace'
    >

    > >>> igg = shutil.ignore_patterns('*.txt', '*.hdf', '*.pdf', '*.png')
    > >>> ignorethat = igg(ddftopdir, os.listdir(ddftopdir))
    > >>> ignorethat

    > set(['Chi2.png', 'DTSdata.hdf', 'TST.hdf', 'BullNoseDiffs.png',
    > 'DTSall.hdf', 'Symmetry.pdf'])

    > >>>

    > ++++++++++++++++++++++++++++
    > Why does it fail on passing in a tuple of ignore strings? I thought the ,
    > (comma) is pretty much the tuple constructor (if that is the right word).
    >
    > How can I solve this? Is there a way to convert a tuple of strings in a form
    > that will be accepted?
    >


    Untested, but I think it should work.
    igf = shutil.ignore_patterns(*ipatterns)

    Ramit

    This email isconfidential and subject to important disclaimers and
    conditions including on offers for the purchase or sale of
    securities, accuracy and completeness of information, viruses,
    confidentiality, legal privilege,and legal entity disclaimers,
    available at http://www.jpmorgan.com/pages/disclosures/email.
     
    Prasad, Ramit, Jul 19, 2012
    #2
    1. Advertising

  3. On Thu, Jul 19, 2012 at 12:43 PM, Alex van der Spek <> wrote:
    > This beats me:
    > ++++++++++++++++++++
    >>>>
    >>>> ipatterns

    >
    > ('*.txt', '*.hdf', '*.pdf', '*.png')
    >>>>
    >>>> igf = shutil.ignore_patterns(ipatterns)
    >>>> ignorethis = igf(ddftopdir,os.listdir(ddftopdir))

    >
    >
    > Traceback (most recent call last):
    > File "<pyshell#60>", line 1, in <module>
    > ignorethis = igf(ddftopdir,os.listdir(ddftopdir))
    > File "C:\Python27\lib\shutil.py", line 138, in _ignore_patterns
    > ignored_names.extend(fnmatch.filter(names, pattern))
    > File "C:\Python27\lib\fnmatch.py", line 49, in filter
    > pat=os.path.normcase(pat)
    > File "C:\Python27\lib\ntpath.py", line 46, in normcase
    > return s.replace("/", "\\").lower()
    > AttributeError: 'tuple' object has no attribute 'replace'


    What is s? It needs to be a string. If s is a tuple with string
    values, you will have to iterate over each to replace. Or I believe
    you could use map.
    >
    >>>> igg = shutil.ignore_patterns('*.txt', '*.hdf', '*.pdf', '*.png')
    >>>> ignorethat = igg(ddftopdir, os.listdir(ddftopdir))
    >>>> ignorethat

    >
    > set(['Chi2.png', 'DTSdata.hdf', 'TST.hdf', 'BullNoseDiffs.png',
    > 'DTSall.hdf', 'Symmetry.pdf'])
    >>>>
    >>>>

    > ++++++++++++++++++++++++++++
    > Why does it fail on passing in a tuple of ignore strings? I thought the ,
    > (comma) is pretty much the tuple constructor (if that is the right word).
    >
    > How can I solve this? Is there a way to convert a tuple of strings in a form
    > that will be accepted?
    >
    > Thank you in advance,
    > Alex van der Spek
    >
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list




    --
    Joel Goldstick
     
    Joel Goldstick, Jul 19, 2012
    #3
  4. Alex van der Spek

    Dave Angel Guest

    On 07/19/2012 12:43 PM, Alex van der Spek wrote:
    > This beats me:
    > ++++++++++++++++++++
    >>>> ipatterns

    > ('*.txt', '*.hdf', '*.pdf', '*.png')
    >>>> igf = shutil.ignore_patterns(ipatterns)
    >>>> ignorethis = igf(ddftopdir,os.listdir(ddftopdir))

    >
    > <SNIP>
    > Why does it fail on passing in a tuple of ignore strings? I thought
    > the , (comma) is pretty much the tuple constructor (if that is the
    > right word).
    >


    ignore_patterns() is not looking for a tuple, it's looking for one or
    more strings, as separate arguments. If you already have the tuple, you
    can expand it by using the asterisk, as Prasad ha pointed out.

    igf = shutil.ignore_patterns(*ipatterns)

    --

    DaveA
     
    Dave Angel, Jul 19, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    812
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    960
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    615
    Bill Scherer
    Apr 20, 2004
  4. Fletcher Johnson

    Why is the shutil module called shutil?

    Fletcher Johnson, Sep 24, 2011, in forum: Python
    Replies:
    11
    Views:
    878
    alex23
    Sep 26, 2011
  5. Rob Meade
    Replies:
    6
    Views:
    268
    Rob Meade
    Mar 1, 2004
Loading...

Share This Page