Re: [OT] Re: output from external commands
On Monday 24 October 2005 09:04 pm, darren kirby wrote: quoth the Fredrik Lundh: (using either on the output from glob.glob is just plain silly, of course) Silly? Sure. os.listdir() is more on point. Never said I was the smartest. However, I will defend my post by pointing out that at the time it was the only one that actually included code that did what the OP wanted. I think Mr. Lundh's point was only that the output from glob.glob is already guaranteed to be strings, so using either '%s'%f or str(f) is superfluous. -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com -- http://mail.python.org/mailman/listinfo/python-list
Re: [OT] Re: output from external commands
Terry Hancock [EMAIL PROTECTED] writes: I think Mr. Lundh's point was only that the output from glob.glob is already guaranteed to be strings, so using either '%s'%f or str(f) is superfluous. Just for the record - this was why I asked what the point was in the first place. mike -- Mike Meyer [EMAIL PROTECTED] http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. -- http://mail.python.org/mailman/listinfo/python-list
Re: [OT] Re: output from external commands
On Tue, 25 Oct 2005 10:10:39 -0500, Terry Hancock [EMAIL PROTECTED] wrote: On Monday 24 October 2005 09:04 pm, darren kirby wrote: quoth the Fredrik Lundh: (using either on the output from glob.glob is just plain silly, of course) Silly? Sure. os.listdir() is more on point. Never said I was the smartest. However, I will defend my post by pointing out that at the time it was the only one that actually included code that did what the OP wanted. I think Mr. Lundh's point was only that the output from glob.glob is already guaranteed to be strings, so using either '%s'%f or str(f) is superfluous. And so is a listcomp that only reproduces the list returned by glob.glob -- especially by iterating through that same returned list ;-) Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list
Re: output from external commands
quoth the James Colannino: Hey everyone. First off, I'm new to the list. I had had a little bit of experience with Perl before discovering Python. The more Python I learn, the more I love it :) I just have a quick question to ask. I know that this is probably a simple question, but I've been googling around, and partly because I'm not sure exactly what to search for, I've been unsuccessful at finding an answer. What I'd like to do is be able to take the output of an external command and assign it as an array of strings. So, for example, in Perl I could do something like: @files = `ls`; So I guess I'm looking for something similiar to the backticks in Perl. Forgive me if I've asked something that's a bit basic for this list. Any help would be greatly appreciated :) Thanks very much in advance. If all you want is filenames this will work: import glob files = [%s % f for f in glob.glob(*)] Else use os.popen to iterate over lines of output: import os for line in os.popen(ls -l).readlines(): . . . process(line) Or check out subprocess if you have 2.4.. James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org ...the number of UNIX installations has grown to 10, with more expected... - Dennis Ritchie and Ken Thompson, June 1972 pgpPYTSvHOmSy.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: output from external commands
darren kirby wrote: quoth the James Colannino: So, for example, in Perl I could do something like: @files = `ls`; So I guess I'm looking for something similiar to the backticks in Perl. Forgive me if I've asked something that's a bit basic for this list. Any help would be greatly appreciated :) Thanks very much in advance. If all you want is filenames this will work: import glob files = [%s % f for f in glob.glob(*)] or import os files = os.listdir('.') Python has built-in support for many file manipulations, see the os, os.path and shutil modules. Kent -- http://mail.python.org/mailman/listinfo/python-list
Re: output from external commands
darren kirby [EMAIL PROTECTED] writes: If all you want is filenames this will work: import glob files = [%s % f for f in glob.glob(*)] What's the point of doing %s % f? How is this different from just file = [f for f in glob.glob(*)]? mike -- Mike Meyer [EMAIL PROTECTED] http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. -- http://mail.python.org/mailman/listinfo/python-list
Re: output from external commands
Mike Meyer wrote: darren kirby [EMAIL PROTECTED] writes: If all you want is filenames this will work: import glob files = [%s % f for f in glob.glob(*)] What's the point of doing %s % f? How is this different from just file = [f for f in glob.glob(*)]? Answering narrowly, the difference is that using %s calls str() on the items in the result list, while your suggestion does not. (Why not just use str(f) instead of the less clear '%s' % f? would be a valid question too though.) -Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: output from external commands
Kent Johnson wrote: import os files = os.listdir('.') Thanks, that's good to know. I still need to use os.popen() for a few things, but I'll be needing filenames also, so when I try to get filenames I'll use the above. James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ If Carpenters made houses the way programmers design programs, the first woodpecker to come along would destroy all of civilization. --Computer Proverb -- http://mail.python.org/mailman/listinfo/python-list
Re: output from external commands
On Monday 24 October 2005 11:24 am, Peter Hansen wrote: Answering narrowly, the difference is that using %s calls str() on the items in the result list, while your suggestion does not. (Why not just use str(f) instead of the less clear '%s' % f? would be a valid question too though.) The answer to which, in my experience, is to provide consistency with code that uses a non-trivial format string, such as 'Error: %s % f', and/or to suggest to future developers (including me) that this is the right way to make such changes. Note also that for those who count, str(f) is exactly as long (in keystrokes) as '%s'%f, making the just a matter of opinion. -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com -- http://mail.python.org/mailman/listinfo/python-list
Re: output from external commands
Terry Hancock wrote: Note also that for those who count, str(f) is exactly as long (in keystrokes) as '%s'%f, making the just a matter of opinion. the % implementation still has to create an overallocated output buffer, parse the format string, call str() on the argument, verify the result, check that the new string fits in the output buffer, copy the contents to the new string to the output buffer, discard the new string, and finally, when the entire format string has been processed, resize the output buffer to the right size. however, the memory allocator is fast, block copies are cheap, and operator access is faster than global function accesses, so as long as the format string is short, the resulting string is no more than 100 bytes longer, and Raymond hasn't gotten around to optimize the str() code path, % can be slightly faster than a plain call to str(). in all other cases, str() is faster. (using either on the output from glob.glob is just plain silly, of course) /F -- http://mail.python.org/mailman/listinfo/python-list
[OT] Re: output from external commands
quoth the Fredrik Lundh: (using either on the output from glob.glob is just plain silly, of course) Silly? Sure. os.listdir() is more on point. Never said I was the smartest. However, I will defend my post by pointing out that at the time it was the only one that actually included code that did what the OP wanted. Recall I wrote: If all you want is filenames this will work: not: This is how you should do it And I invite you to prove me wrong ... it does work. As a novice, I do appreciate getting set strait when I code something dumb, but going off about the efficiency of %s % foo over str(foo) hardly helps the OP, and is not very pertinant to my glob faux pas either. An explanation of why glob is silly would perhaps teach me better than just stating it as fact. It is things like this that make me wary of posting to this list, either to help another, or with my own q's. All I usually want is help with a specific problem, not a critique involving how brain-dead my code is. I'm a beginner, of course my code is going to be brain-dead ;) I thought the idea was make it work first, then optimize? In any event, I will refrain from trying to help people here until I get over this silly stage I seem to be stuck in... it just doesn't seem worth it. I am not trying to sound like a whiner here, I just wish you experts would go easy on us novices... -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org ...the number of UNIX installations has grown to 10, with more expected... - Dennis Ritchie and Ken Thompson, June 1972 pgprtsZtFVPaz.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: [OT] Re: output from external commands
darren kirby wrote: quoth the Fredrik Lundh: (using either on the output from glob.glob is just plain silly, of course) [snip] It is things like this that make me wary of posting to this list, either to help another, or with my own q's. All I usually want is help with a specific problem, not a critique involving how brain-dead my code is. I'm a beginner, of course my code is going to be brain-dead ;) I wouldn't fret too much about a sharp remark from Fredrik Lundh. They're pretty much all that way. ;) It looks like you already did the right thing - read past the insults, and gleaned the useful information that he included in between. It takes a little training to get used to him, but if you can look past the nasty bite, he's really a valuable resource around here. STeVe -- http://mail.python.org/mailman/listinfo/python-list
Re: [OT] Re: output from external commands
On 25/10/2005, at 3:36 PM, Steven Bethard wrote: I wouldn't fret too much about a sharp remark from Fredrik Lundh. They're pretty much all that way. ;) [...] It takes a little training to get used to him, but if you can look past the nasty bite, he's really a valuable resource around here. +1 QOTW :) =Tony.Meyer -- http://mail.python.org/mailman/listinfo/python-list
Re: output from external commands
James Colannino [EMAIL PROTECTED] writes: Hey everyone. First off, I'm new to the list. I had had a little bit of experience with Perl before discovering Python. The more Python I learn, the more I love it :) I just have a quick question to ask. I know that this is probably a simple question, but I've been googling around, and partly because I'm not sure exactly what to search for, I've been unsuccessful at finding an answer. What I'd like to do is be able to take the output of an external command and assign it as an array of strings. So, for example, in Perl I could do something like: @files = `ls`; So I guess I'm looking for something similiar to the backticks in Perl. Forgive me if I've asked something that's a bit basic for this list. Any help would be greatly appreciated :) Thanks very much in advance. This is a scripting language feature. Python doesn't have direct support for it, any more than C++ does. To get that functionality, you want to use either the os.popen function, or - preferable, but only available in newer Pythons - the subprocess module. mike -- Mike Meyer [EMAIL PROTECTED] http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. -- http://mail.python.org/mailman/listinfo/python-list
Re: output from external commands
Mike Meyer wrote: This is a scripting language feature. Python doesn't have direct support for it, any more than C++ does. To get that functionality, you want to use either the os.popen function, or - preferable, but only available in newer Pythons - the subprocess module. Thanks. James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ -- http://mail.python.org/mailman/listinfo/python-list