Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!news2.euro.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'sure.': 0.05; 'whichever': 0.07; '>>>>': 0.09; 'filename': 0.09; 'from:addr:python': 0.09; 'skip:[ 30': 0.09; 'subject:files': 0.09; 'subject:file': 0.13; 'def': 0.15; 'comma': 0.16; 'filenames': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply- to:addr:python-list': 0.16; 'sally': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'this?': 0.21; 'header:In-Reply-To:1': 0.22; 'picking': 0.23; 'worked': 0.23; 'aug': 0.24; 'code': 0.25; 'code.': 0.26; 'tried': 0.26; 'van': 0.26; 'random': 0.28; 'separate': 0.28; 'received:84': 0.28; 'sat,': 0.28; 'import': 0.28; 'fix': 0.29; 'asking': 0.29; 'print': 0.29; 'lines': 0.30; 'list': 0.32; 'this.': 0.32; 'too': 0.33; 'to:addr:python-list': 0.33; 'named': 0.33; 'header:User-Agent:1': 0.34; 'directory.': 0.34; 'like:': 0.34; 'reply-to:addr:python.org': 0.34; 'thank': 0.35; 'subject:text': 0.35; 'file': 0.36; 'question': 0.36; 'pull': 0.37; 'listed': 0.37; 'using': 0.37; 'but': 0.37; 'something': 0.37; 'could': 0.38; 'hello,': 0.38; 'some': 0.38; 'move': 0.38; 'skip:o 20': 0.38; 'subject:: ': 0.39; 'persons': 0.39; 'format.': 0.39; 'data': 0.39; 'to:addr:python.org': 0.39; "i'd": 0.40; 'more': 0.60; 'hope': 0.61; 'john': 0.62; 'below': 0.62; 'alternative': 0.64; 'business': 0.70; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.71; 'encountered': 0.73; '100': 0.73; 'username': 0.77; '10:03': 0.84; 'directories,': 0.84; 'stone,': 0.84; 'stone': 0.91; 'orders,': 0.95 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Av4EADSBWU7Unw4S/2dsb2JhbABCqA93gUABAQU4QBELCBAJFg8JAwIBAgENOBMGAgEBh3CtJIpxhkwEi2RJjACLdw Date: Sun, 28 Aug 2011 00:48:20 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:6.0) Gecko/20110812 Thunderbird/6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Arrange files according to a text file References: <6j8i57t6cgqunn3c1ci4p7u9mnpnvsrl8s@4ax.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 127 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314488964 news.xs4all.nl 2451 [2001:888:2000:d::a6]:56173 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12320 On 28/08/2011 00:18, Ric@rdo.python.org wrote: > Thank you so much. The code worked perfectly. > > This is what I tried using Emile code. The only time when it picked > wrong name from the list was when the file was named like this. > > Data Mark Stone.doc > > How can I fix this? Hope I am not asking too much? > Have you tried the alternative word orders, "Mark Stone" as well as "Stone, Mark", picking whichever name has the best ratio for either? > > import os > from difflib import SequenceMatcher as SM > > path = r'D:\Files ' > txt_names = [] > > > with open(r'D:/python/log1.txt') as f: > for txt_name in f.readlines(): > txt_names.append(txt_name.strip()) > > def ignore(x): > return x in ' ,.' > > for filename in os.listdir(path): > ratios = [SM(ignore,filename,txt_name).ratio() for txt_name in > txt_names] > best = max(ratios) > owner = txt_names[ratios.index(best)] > print filename,":",owner > > > > > > On Sat, 27 Aug 2011 14:08:17 -0700, Emile van Sebille > wrote: > >> On 8/27/2011 1:15 PM Ric@rdo.python.org said... >>> >>> Hello Emile , >>> >>> Thank you for the code below as I have not encountered SequenceMatcher >>> before and would have to take a look at it closer. >>> >>> My question would it work for a text file list of names about 25k >>> lines and a directory with say 100 files inside? >> >> Sure. >> >> Emile >> >> >>> >>> Thank you once again. >>> >>> >>> On Sat, 27 Aug 2011 11:06:22 -0700, Emile van Sebille >>> wrote: >>> >>>> On 8/27/2011 10:03 AM Ric@rdo.python.org said... >>>>> Hello, >>>>> >>>>> What would be the best way to accomplish this task? >>>> >>>> I'd do something like: >>>> >>>> >>>> usernames = """Adler, Jack >>>> Smith, John >>>> Smith, Sally >>>> Stone, Mark""".split('\n') >>>> >>>> filenames = """Smith, John - 02-15-75 - business files.doc >>>> Random Data - Adler Jack - expenses.xls >>>> More Data Mark Stone files list.doc""".split('\n') >>>> >>> >from difflib import SequenceMatcher as SM >>>> >>>> >>>> def ignore(x): >>>> return x in ' ,.' >>>> >>>> >>>> for filename in filenames: >>>> ratios = [SM(ignore,filename,username).ratio() for username in >>>> usernames] >>>> best = max(ratios) >>>> owner = usernames[ratios.index(best)] >>>> print filename,":",owner >>>> >>>> >>>> Emile >>>> >>>> >>>> >>>>> I have many files in separate directories, each file name >>>>> contain a persons name but never in the same spot. >>>>> I need to find that name which is listed in a large >>>>> text file in the following format. Last name, comma >>>>> and First name. The last name could be duplicate. >>>>> >>>>> Adler, Jack >>>>> Smith, John >>>>> Smith, Sally >>>>> Stone, Mark >>>>> etc. >>>>> >>>>> >>>>> The file names don't necessary follow any standard >>>>> format. >>>>> >>>>> Smith, John - 02-15-75 - business files.doc >>>>> Random Data - Adler Jack - expenses.xls >>>>> More Data Mark Stone files list.doc >>>>> etc >>>>> >>>>> I need some way to pull the name from the file name, find it in the >>>>> text list and then create a directory based on the name on the list >>>>> "Smith, John" and move all files named with the clients name into that >>>>> directory. >>>> >>