Arrange files according to a text file
Hello, What would be the best way to accomplish this task? 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Arrange files according to a text file
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? Thank you once again. On Sat, 27 Aug 2011 11:06:22 -0700, Emile van Sebille wrote: >On 8/27/2011 10:03 AM r...@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. > -- http://mail.python.org/mailman/listinfo/python-list
Re: Arrange files according to a text file
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? 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 r...@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 r...@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. >>> > -- http://mail.python.org/mailman/listinfo/python-list
Re: Arrange files according to a text file
On Sun, 28 Aug 2011 00:48:20 +0100, MRAB wrote: >On 28/08/2011 00:18, r...@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? >> Yes I tried and the result was the same. I will try to work out something. thank you. >> 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 r...@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 r...@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. > >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: Arrange files according to a text file
No, it turned out to be my mistake. Your code was correct and I appreciate it very much. Thank you again On Sat, 27 Aug 2011 18:10:07 -0700, Emile van Sebille wrote: >On 8/27/2011 4:18 PM r...@rdo.python.org said... >> 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? > >What name did it pick? I imagine if you're picking a name from a list >of 25000 names that some subset of combinations may yield like ratios. > >But, if you double up on the file name side you may get closer: > >for filename in filenames: > ratios = [SM(ignore,filename+filename,username).ratio() for >username in usernames] > best = max(ratios) > owner = usernames[ratios.index(best)] > print filename,":",owner > >... on the other hand, if you've only got a 100 files to sort out, you >should already be done. > >:) > >Emile -- http://mail.python.org/mailman/listinfo/python-list
ttk Listbox
What would be an equivalent widget in ttk like a Listbox and if possible a small example? I tried to look here http://docs.python.org/library/ttk.html but did not see anything. Maybe I did not look in the right place? tia -- http://mail.python.org/mailman/listinfo/python-list
Re: ttk Listbox
On Mon, 31 Oct 2011 10:00:22 -0400, Kevin Walzer wrote: >On 10/31/11 12:37 AM, Ric@rdo wrote: >> >> What would be an equivalent widget in ttk like a Listbox and if >> possible a small example? I tried to look here >> http://docs.python.org/library/ttk.html but did not see anything. >> >> Maybe I did not look in the right place? >> >> tia > >The listbox isn't part of the themed ttk widgets. The ttk::treview is, >and that can be set up as a single-column list display. There may be an >example of how to do this in the docs or source code tree (I don't use >the widget myself so I don't have any sample code to share). > >--Kevin Thank you for the information. -- http://mail.python.org/mailman/listinfo/python-list
Re: ttk Listbox
On Mon, 31 Oct 2011 10:00:22 -0400, Kevin Walzer wrote: >On 10/31/11 12:37 AM, Ric@rdo wrote: >> >> What would be an equivalent widget in ttk like a Listbox and if >> possible a small example? I tried to look here >> http://docs.python.org/library/ttk.html but did not see anything. >> >> Maybe I did not look in the right place? >> >> tia > >The listbox isn't part of the themed ttk widgets. The ttk::treview is, >and that can be set up as a single-column list display. There may be an >example of how to do this in the docs or source code tree (I don't use >the widget myself so I don't have any sample code to share). > >--Kevin Quick question: Then why is it mentioned here http://www.tkdocs.com/tutorial/morewidgets.html? Listboxes are created using the Listbox function: l = Listbox(parent, height=10) -- http://mail.python.org/mailman/listinfo/python-list
Sort items in wxListCtrl
I am trying to create a small application in wxPython and would like to ask for some help. I am trying to display folders and files in ListCtrl but sorted first folders followed by files (like in a file manager style) but not sure how to do this? Would I need to do this in code somehow or ListCtrl would help me? I am trying to do this and learn at the same time. Would appreciate any advice. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Sort items in wxListCtrl
On Tue, 1 Nov 2011 16:14:50 -0700 (PDT), Miki Tebeka wrote: >Why not use the build in wx.FileDialog? > >Also, have a look at the demo that comes with wxPython. It has an example with >a sortable list control. Thanks for responding, How would wx.FileDialog help me in this case? I am trying to display files and directories in the ListCtrl in sorted way. Folders first followed by files. -- http://mail.python.org/mailman/listinfo/python-list
Re: Sort items in wxListCtrl
On Wed, 2 Nov 2011 08:08:48 -0700 (PDT), Miki Tebeka wrote: >wx.FileDialog shows files and directories. If you need the user to pick one, >this is the standard way. Otherwise, if you need custom view on the file >system then probably list control is the right way to go. Again, the demo has >a working example with sortable list control. Thanks, Do you know if List control has an click event that if you double click on a directory displayed in the control an action can occur? Sorry if its right there but I just did not see. -- http://mail.python.org/mailman/listinfo/python-list
Returning histogram-like data for items in a list
Hi there, I have a list: L1 = [1,1,1,2,2,3] How can I easily turn this into a list of tuples where the first element is the list element and the second is the number of times it occurs in the list (I think that this is referred to as a histogram): i.e.: L2 = [(1,3),(2,2),(3,1)] I was doing something like: myDict = {} for i in L1: myDict.setdefault(i,[]).append(i) then doing this: L2 = [] for k, v in myDict.iteritems(): L2.append((k, len(v))) This works but I sort of feel like there ought to be an easier way, rather than to have to store the list elements, when all I want is a count of them. Would anyone care to comment? I also tried this trick, where locals()['_[1]'] refers to the list comprehension itself as it gets built, but it gave me unexpected results: >>> L2 = [(i, len(i)) for i in L2 if not i in locals()['_[1]']] >>> L2 [((1, 3), 2), ((2, 2), 2), ((3, 1), 2)] i.e. I don't understand why each tuple is being counted as well. Regards, Ric -- http://mail.python.org/mailman/listinfo/python-list
What are __slots__ used for?
I am a C# programmer and new to the language and I am trying to debug some code which uses this feature. Can anyone elaborate on what it is and how it is used? Regards, Ric -- http://mail.python.org/mailman/listinfo/python-list
Tricky Dictionary Question from newbie
Hi all, I have a dictionary containing about 300 items, some of the values being repeated. Both keys and values are strings. How can I turn this thing on its head so that we create a key based on each unique value and build the values based on the keys corresponding to the repeated values? It is hard to explain but this is what I mean: Dict = {'rt': 'This is repeated', 'sr': 'This is repeated', 'gf': 'This is not'} I want this to return a new dict with string keys and lists containing the previous keys for repeated values. NewDict = {'This is repeated':['rt','sr'],'This is not':['gf']} I am still learning Python and have struggled with this for hours before deciding to go for help. Unfortunately, I didn't really know how to search for this in google and decided to post it here. I apologise if this is too basic for this newsgroup... Ric -- http://mail.python.org/mailman/listinfo/python-list
Re: Tricky Dictionary Question from newbie
Thank you guys! (Reinhold, Mark and Markus) I must confess that I am absolutely awe struck at the power of this language! There is no way in the world that I would have envisaged such simple and elegant solutions!!! Reinhold, is your solution specific to 2.4? Kind Regards, Ric "Reinhold Birkenfeld" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Mark Jackson wrote: >> "Ric Da Force" <[EMAIL PROTECTED]> writes: >> >>> It is hard to explain but this is what I mean: >>> >>> Dict = {'rt': 'This is repeated', 'sr': 'This is repeated', 'gf': 'This >>> is >>> not'} >>> >>> I want this to return a new dict with string keys and lists containing >>> the >>> previous keys for repeated values. >>> >>> NewDict = {'This is repeated':['rt','sr'],'This is not':['gf']} >> >> NewDict = {} >> for x in Dict.keys(): >> try: >> NewDict[Dict[x]].append(x) >> except KeyError: >> NewDict[Dict[x]] = [x] > > Or, more up-to-date: > > NewDict = {} > for key, val in Dict.iteritems(): >NewDict.setdefault(val, []).append(key) > > Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: Tricky Dictionary Question from newbie
How does setdefault work exactly? I am looking in the docs and can't figure it out... Ric "Ric Da Force" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Thank you guys! (Reinhold, Mark and Markus) I must confess that I am > absolutely awe struck at the power of this language! There is no way in > the world that I would have envisaged such simple and elegant solutions!!! > > Reinhold, is your solution specific to 2.4? > > Kind Regards, > > Ric > > "Reinhold Birkenfeld" <[EMAIL PROTECTED]> wrote in > message news:[EMAIL PROTECTED] >> Mark Jackson wrote: >>> "Ric Da Force" <[EMAIL PROTECTED]> writes: >>> >>>> It is hard to explain but this is what I mean: >>>> >>>> Dict = {'rt': 'This is repeated', 'sr': 'This is repeated', 'gf': 'This >>>> is >>>> not'} >>>> >>>> I want this to return a new dict with string keys and lists containing >>>> the >>>> previous keys for repeated values. >>>> >>>> NewDict = {'This is repeated':['rt','sr'],'This is not':['gf']} >>> >>> NewDict = {} >>> for x in Dict.keys(): >>> try: >>> NewDict[Dict[x]].append(x) >>> except KeyError: >>> NewDict[Dict[x]] = [x] >> >> Or, more up-to-date: >> >> NewDict = {} >> for key, val in Dict.iteritems(): >>NewDict.setdefault(val, []).append(key) >> >> Reinhold > > -- http://mail.python.org/mailman/listinfo/python-list
Replacing last comma in 'C1, C2, C3' with 'and' so that it reads 'C1, C2 and C3'
Hi, I have a string such as 'C1, C2, C3'. Without assuming that each bit of text is of fixed size, what is the easiest way to change this list so that it reads: 'C1, C2 and C3' regardless of the length of the string. Regards and sorry for the newbie question, Ric -- http://mail.python.org/mailman/listinfo/python-list
Re: Replacing last comma in 'C1, C2, C3' with 'and' so that it reads'C1, C2 and C3'
Hi guys, Thank you all for your input! It was good to see so much convergence in the approach! Again, I think that it speaks loudly for the concise way of doing thins in Python... Anyway, I have typed in all of the solutions and have gained a great understanding of how to do this in future. Thanks again! Ric "Brian van den Broek" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Ric Da Force said unto the world upon 12/07/2005 02:43: >> Hi, >> >> I have a string such as 'C1, C2, C3'. Without assuming that each bit of >> text is of fixed size, what is the easiest way to change this list so >> that it reads: >> 'C1, C2 and C3' regardless of the length of the string. >> >> Regards and sorry for the newbie question, >> >> Ric > > Hi Ric, > > the rsplit method of strings should get you going: > > >>> data = "the first bit, then the second, finally the third" > >>> chunks = data.rsplit(',', 1) > >>> chunks > ['the first bit, then the second', ' finally the third'] > >>> > > Best, > > Brian vdB > > -- http://mail.python.org/mailman/listinfo/python-list