Re: Partial directory search question
chad cdal...@gmail.com wrote in message news:4e260ef3-8b0e-4613-a4f8-1c267e875...@u16g2000pru.googlegroups.com... On Sep 29, 7:20 pm, Tim Chase python.l...@tim.thechases.com wrote: What's the sanest way to print out all the files in the directory that start with the underscore? Ie, I just want to list _1, _2, _3, _4. I'd use a string's join() method to combine the results of a list-comprehension or generator that filtered the output of os.listdir() based on the startswith() method of the strings. Left intentionally oblique and code-free because this sounds a bit like a home-work problem. If you're a python coder, that should make pretty decent sense and be a one-liner to implement. -tkc Okay, sorry for the delay to the response. I got side tracked trying to stalk, I mean talk to the 59 year old neighbor girl. Anyways, I couldn't get it to one in one line. Here is what I did... % more rec.py #!/usr/local/bin/python import os import time for filename in os.listdir(/usr/bbs/confs/september): #stat = os.stat(filename) if filename.startswith(_): print filename L = [filename for filename in os.listdir(/usr/bbs/confs/september) if filename.startswith(_)] Now you have a list with the desired filtered names. ./rec.py _1 _2 _3 _4 _5 _6 _7 _8 It correctly prints out all the files in the directory that start with an underscore. -- http://mail.python.org/mailman/listinfo/python-list
Re: Partial directory search question
import os for filename in os.listdir(/usr/bbs/confs/september): #stat = os.stat(filename) if filename.startswith(_): print filename yes, as lallous mentioned, this can be done as a list-comprehension/generator. If printing is all you want to do, it's a nice and concise way: print '\n'.join(fname for fname in os.listdir(loc) if fname.startswith('_')) If you're doing more processing than just printing it, your for-loop is a better (clearer) way to go. If you have lots of processing code, it might help to do the inverse: for filename in os.listdir(location): if not filename.startswith('_'): continue lots() of_processing() and_your_complex_logic() goes() here() It removes one level of indentation depth and makes it clear that you don't intend to do anything with the non-leading-underscore versions (rather than looking for a corresponding else: line possibly screens later). -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Partial directory search question
Rami Chowdhury Never attributed to malice that which can be attributed to stupidity. -- Hanlon's Razor 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) On Tuesday 29 September 2009 19:54:17 chad wrote: On Sep 29, 7:52 pm, chad cdal...@gmail.com wrote: On Sep 29, 7:20 pm, Tim Chase python.l...@tim.thechases.com wrote: What's the sanest way to print out all the files in the directory that start with the underscore? Ie, I just want to list _1, _2, _3, _4. I'd use a string's join() method to combine the results of a list-comprehension or generator that filtered the output of os.listdir() based on the startswith() method of the strings. Left intentionally oblique and code-free because this sounds a bit like a home-work problem. If you're a python coder, that should make pretty decent sense and be a one-liner to implement. -tkc Okay, sorry for the delay to the response. I got side tracked trying to stalk, I mean talk to the 59 year old neighbor girl. Anyways, I couldn't get it to one in one line. Here is what I did... % more rec.py #!/usr/local/bin/python import os import time for filename in os.listdir(/usr/bbs/confs/september): #stat = os.stat(filename) if filename.startswith(_): print filename ./rec.py _1 _2 _3 _4 _5 _6 _7 _8 It correctly prints out all the files in the directory that start with an underscore. er *couldn't get it into a one liner*. To get it into one line, I suggest: http://docs.python.org/tutorial/datastructures.html#list-comprehensions -- http://mail.python.org/mailman/listinfo/python-list
Re: Partial directory search question
Tim Chase python.l...@tim.thechases.com wrote: If you're doing more processing than just printing it, your for-loop is a better (clearer) way to go. If you have lots of processing code, it might help to do the inverse: for filename in os.listdir(location): if not filename.startswith('_'): continue lots() of_processing() and_your_complex_logic() goes() here() Personally, I'd still go with a generator to drive the for-loop: underscored_files = (f for f in os.listdir(location) if not f.startswith('_')) for filename in underscored_files: etc... What I'm traversing in the for-loop is far more obvious (to me) from the name of the generator than from having to parse the first few lines of the loop. It's a lot easier to genericise that behaviour too. -- http://mail.python.org/mailman/listinfo/python-list
Re: Partial directory search question
What's the sanest way to print out all the files in the directory that start with the underscore? Ie, I just want to list _1, _2, _3, _4. I'd use a string's join() method to combine the results of a list-comprehension or generator that filtered the output of os.listdir() based on the startswith() method of the strings. Left intentionally oblique and code-free because this sounds a bit like a home-work problem. If you're a python coder, that should make pretty decent sense and be a one-liner to implement. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Partial directory search question
On Sep 29, 7:20 pm, Tim Chase python.l...@tim.thechases.com wrote: What's the sanest way to print out all the files in the directory that start with the underscore? Ie, I just want to list _1, _2, _3, _4. I'd use a string's join() method to combine the results of a list-comprehension or generator that filtered the output of os.listdir() based on the startswith() method of the strings. Left intentionally oblique and code-free because this sounds a bit like a home-work problem. If you're a python coder, that should make pretty decent sense and be a one-liner to implement. -tkc Okay, sorry for the delay to the response. I got side tracked trying to stalk, I mean talk to the 59 year old neighbor girl. Anyways, I couldn't get it to one in one line. Here is what I did... % more rec.py #!/usr/local/bin/python import os import time for filename in os.listdir(/usr/bbs/confs/september): #stat = os.stat(filename) if filename.startswith(_): print filename ./rec.py _1 _2 _3 _4 _5 _6 _7 _8 It correctly prints out all the files in the directory that start with an underscore. -- http://mail.python.org/mailman/listinfo/python-list
Re: Partial directory search question
On Sep 29, 7:52 pm, chad cdal...@gmail.com wrote: On Sep 29, 7:20 pm, Tim Chase python.l...@tim.thechases.com wrote: What's the sanest way to print out all the files in the directory that start with the underscore? Ie, I just want to list _1, _2, _3, _4. I'd use a string's join() method to combine the results of a list-comprehension or generator that filtered the output of os.listdir() based on the startswith() method of the strings. Left intentionally oblique and code-free because this sounds a bit like a home-work problem. If you're a python coder, that should make pretty decent sense and be a one-liner to implement. -tkc Okay, sorry for the delay to the response. I got side tracked trying to stalk, I mean talk to the 59 year old neighbor girl. Anyways, I couldn't get it to one in one line. Here is what I did... % more rec.py #!/usr/local/bin/python import os import time for filename in os.listdir(/usr/bbs/confs/september): #stat = os.stat(filename) if filename.startswith(_): print filename ./rec.py _1 _2 _3 _4 _5 _6 _7 _8 It correctly prints out all the files in the directory that start with an underscore. er *couldn't get it into a one liner*. -- http://mail.python.org/mailman/listinfo/python-list
Re: Partial directory search question
On Tue, 29 Sep 2009 19:09:16 -0700 (PDT), chad cdal...@gmail.com wrote: I have a directory that contains the following login rc sum _1 _2 _3 _4 What's the sanest way to print out all the files in the directory that start with the underscore? Ie, I just want to list _1, _2, _3, _4. I don't know what you mean by sanest, but this is one way: import glob for f in glob.glob(_*): print f Martien -- | Martien Verbruggen | You can't have everything, where would first.l...@heliotrope.com.au | you put it? | -- http://mail.python.org/mailman/listinfo/python-list