newbie question

Jeremy Jones zanesdad at bellsouth.net
Wed Nov 17 11:26:17 EST 2004


Paul Watson wrote:

>"Jeremy Jones" <zanesdad at bellsouth.net> wrote in message 
>news:mailman.6431.1100623812.5135.python-list at python.org...
>  
>
>>John wrote:
>>
>>    
>>
>>>Can python do Shell programming or system programming as perl does?
>>>Thanks.
>>>
>>>John
>>>
>>>      
>>>
>>Yes.  The only problem is that the perpetual migraine you have from doing 
>>Perl and shell will quickly go away.  So, if you're into pain, stick with 
>>Perl and shell.  If you want relief, come to Python.
>>
>>Seriously, though, Python is an excellent language for doing sysadmin type 
>>tasks. (I'm assuming that's kind of what you wanted?)  Most sysadmin type 
>>tasks are in modules whereas with Perl, they're cluttering the toplevel 
>>namespace (I don't think Perl has the concept of namespace, but you get 
>>the drift).  So, not knowing what you want to do, you may want to checkout 
>>the os module in Python (I've found os.path.* to be specifically helpful). 
>>You may also want to checkout the shutil module.
>>Jeremy
>>    
>>
>
>While I usually use Python, I must admit that doing:
>    $s = `ls -al $thefilename`
>seems easier and clearer than:
>    import os
>    s = os.popen("ls -al %s" % thefilename) 
>
>
>  
>
I find that :

import glob
s = glob.glob(thefilename)

works pretty well. 

If you need to iterate over a bunch of files in a directory, you can do 
this in shell:

for f in `ls *`
do
    #do stuff
done

or

for f in *
do
    #do stuff
done

and in Python:

import os
for f in os.listdir('.'):
    pass # do stuff here

or

import glob
for i in glob.glob('ipythonrc-*'):
    pass #do stuff here

Some things, mostly very simple tasks, shell is fine for.  When you 
start increasing in complexity, I think you're better off using Python.  
The samples above are *quite* simple and you'd probably be just as well 
of doing them in shell as in Python.  Actually better from a shell 
prompt rather than a script.


Jeremy Jones
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20041117/55593369/attachment.html>


More information about the Python-list mailing list