#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    22
    Rep Power
    0

    Exclamation [Python 3 + FNMatch] PNG Searcher not working... help needed!


    Hey Python Programmers...

    I try to create an script as an test, thad must be find PNG Files by name...
    This is my script...

    Code:
    # Import fnmatch and os
    import fnmatch
    import os
    
    # Search PNG-File by Name:
    filesearch = input('Search File: ')
    for file in os.walk(filesearch):
        if fnmatch.filter(file, '*.png'):
            print(file)
    If i call in the name of any PNG Files, this script doesn't works...
    No PNG file found wich starts with the first letter you call, they i get on my
    computer (laptop)..... I use python 3....

    I try to make an PNG-Searcher (or specific file-searcher), they must be find
    PNG Files by name (or the png-files they get the same letters)...

    If i run my script, no files found with the begin-letters or same names...
    I get realy png-files where i get testing it... to look of my script works...

    Can anyone give my the just code, so i can expiriment with it...
    I am in the learn fase of python (or programming)...

    Thanks for my code correction...
    Jamie.
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,915
    Rep Power
    509
    The arguments to fnmatch.filter are a list and (I presume) a globbing.glob style regular expression. (filename matching pattern) Hence:
    Code:
    >>> fnmatch.filter(['horse.fly', 'house.fly'],'horse*')
    ['horse.fly']
    And you can see that os.walk returns a nested list:
    Code:
    >>> os.chdir('/tmp')
    >>> import pprint
    >>> pprint.pprint([file for file in os.walk(os.curdir)])
    [('.',
      ['.com.google.Chrome.ccfBLb',
       'systemd-private-af43573ce38f41d288f35735f44019d6-systemd-resolved.service-3mRoER',
       '.XIM-unix',
       'systemd-private-af43573ce38f41d288f35735f44019d6-apache2.service-0mKaXr',
       '.X11-unix',
       '.Test-unix',
       '.ICE-unix',
       'systemd-private-af43573ce38f41d288f35735f44019d6-fwupd.service-Xh9wyA',
       'docview1000',
       'launchpadlib.cache.wl63h22s',
       '.font-unix',
       'mozilla_lambertdw0',
       'hsperfdata_lambertdw',
       'thunderbird_lambertdw',
       'ssh-b6BLCRmIrbZn',
       'systemd-private-af43573ce38f41d288f35735f44019d6-rtkit-daemon.service-QUMCl1',
       'systemd-private-af43573ce38f41d288f35735f44019d6-colord.service-3lVy4V',
       'systemd-private-af43573ce38f41d288f35735f44019d6-systemd-timesyncd.service-8wO1xR'],
      ['geIconCacheLock',
       'Makefile',
       'unity_support_test.0',
       'GeckoChildCrash30155.extra',
       'config-err-ZA1PRp',
       'geColladaModelCacheLock',
       'p.py']),
     ('./.com.google.Chrome.ccfBLb', [], ['SingletonSocket', 'SingletonCookie']),
     ('./.XIM-unix', [], []),
     ('./.X11-unix', [], ['X1', 'X0']),
     ('./.Test-unix', [], []),
     ('./.ICE-unix', [], ['1611', '2225']),
     ('./docview1000',
      ['2017 Ministry Plan - 20170511 '
       'revisions.docx-0606cf14fd83a1dc0e3469b8ea1d1220'],
      []),
     ('./docview1000/2017 Ministry Plan - 20170511 '
      'revisions.docx-0606cf14fd83a1dc0e3469b8ea1d1220',
      [],
      ['resolution.el', 'doc.pdf', 'page-2.png', 'page-1.png']),
     ('./launchpadlib.cache.wl63h22s', [], []),
     ('./.font-unix', [], []),
     ('./mozilla_lambertdw0', [], []),
     ('./hsperfdata_lambertdw', [], []),
     ('./thunderbird_lambertdw', [], ['.parentlock']),
     ('./ssh-b6BLCRmIrbZn', [], ['agent.2007'])]
    >>>
    So you've got two problems. Using os.walk directly is incorrect. And when it returns a string, the filter function is trying to match one character at a time.

    >>> print([c for c in 'long string'])
    ['l', 'o', 'n', 'g', ' ', 's', 't', 'r', 'i', 'n', 'g']
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    22
    Rep Power
    0
    Originally Posted by b49P23TIvg
    The arguments to fnmatch.filter are a list and (I presume) a globbing.glob style regular expression. (filename matching pattern) Hence:
    Code:
    >>> fnmatch.filter(['horse.fly', 'house.fly'],'horse*')
    ['horse.fly']
    And you can see that os.walk returns a nested list:
    Code:
    >>> os.chdir('/tmp')
    >>> import pprint
    >>> pprint.pprint([file for file in os.walk(os.curdir)])
    [('.',
      ['.com.google.Chrome.ccfBLb',
       'systemd-private-af43573ce38f41d288f35735f44019d6-systemd-resolved.service-3mRoER',
       '.XIM-unix',
       'systemd-private-af43573ce38f41d288f35735f44019d6-apache2.service-0mKaXr',
       '.X11-unix',
       '.Test-unix',
       '.ICE-unix',
       'systemd-private-af43573ce38f41d288f35735f44019d6-fwupd.service-Xh9wyA',
       'docview1000',
       'launchpadlib.cache.wl63h22s',
       '.font-unix',
       'mozilla_lambertdw0',
       'hsperfdata_lambertdw',
       'thunderbird_lambertdw',
       'ssh-b6BLCRmIrbZn',
       'systemd-private-af43573ce38f41d288f35735f44019d6-rtkit-daemon.service-QUMCl1',
       'systemd-private-af43573ce38f41d288f35735f44019d6-colord.service-3lVy4V',
       'systemd-private-af43573ce38f41d288f35735f44019d6-systemd-timesyncd.service-8wO1xR'],
      ['geIconCacheLock',
       'Makefile',
       'unity_support_test.0',
       'GeckoChildCrash30155.extra',
       'config-err-ZA1PRp',
       'geColladaModelCacheLock',
       'p.py']),
     ('./.com.google.Chrome.ccfBLb', [], ['SingletonSocket', 'SingletonCookie']),
     ('./.XIM-unix', [], []),
     ('./.X11-unix', [], ['X1', 'X0']),
     ('./.Test-unix', [], []),
     ('./.ICE-unix', [], ['1611', '2225']),
     ('./docview1000',
      ['2017 Ministry Plan - 20170511 '
       'revisions.docx-0606cf14fd83a1dc0e3469b8ea1d1220'],
      []),
     ('./docview1000/2017 Ministry Plan - 20170511 '
      'revisions.docx-0606cf14fd83a1dc0e3469b8ea1d1220',
      [],
      ['resolution.el', 'doc.pdf', 'page-2.png', 'page-1.png']),
     ('./launchpadlib.cache.wl63h22s', [], []),
     ('./.font-unix', [], []),
     ('./mozilla_lambertdw0', [], []),
     ('./hsperfdata_lambertdw', [], []),
     ('./thunderbird_lambertdw', [], ['.parentlock']),
     ('./ssh-b6BLCRmIrbZn', [], ['agent.2007'])]
    >>>
    So you've got two problems. Using os.walk directly is incorrect. And when it returns a string, the filter function is trying to match one character at a time.

    >>> print([c for c in 'long string'])
    ['l', 'o', 'n', 'g', ' ', 's', 't', 'r', 'i', 'n', 'g']
    OK, thanks for help, but my code works for my, see under:
    Code:
    import fnmatch
    import os
    
    # Search PNG from Input:
    search = input("File Name: ") + '*.png'
    for file in os.listdir('.'):
        if fnmatch.fnmatch(file, search):
            print(file)
    Find 4 Type Image-Files, even this work for my:
    Code:
    import fnmatch
    import os
    
    # Search PNG, JPG, BMP and TIFF with an input:
    search = input("File Name: ")
    FindPNG = search + '*.png'
    FindJPG = search + '*.jpg'
    FindBMP = search + '*.bmp'
    FindTIF = search + '*.tif'
    
    for file in os.listdir('.'):
        if (((fnmatch.fnmatch(file, FindPNG) or fnmatch.fnmatch(file, FindJPG)) or ((fnmatch.fnmatch(file, FindBMP) or fnmatch.fnmatch(file, FindTIF))) :
            print(file)

    This problem is fixed... but thanks for help...
    Jamie.

IMN logo majestic logo threadwatch logo seochat tools logo