strange behavior with os.system

kmw wuerzner at gmail.com
Tue Jun 16 13:11:52 EDT 2009


Hi,

I wanted to write a simple script (in 5 minutes or so) which replaces
the option '+1' given to the command 'sort' by '-k 2' and than runs
'sort' with the modified argument list. After two hours I am giving up
and ask you for help. This is what I tried (please excuse the verbose
code, it is due to my various efforts to understand the error):

#!/usr/bin/python
import sys, os, re
arguments = sys.argv[0]
for i in sys.argv[1:]:
	arguments += " " + i
p = re.compile ( "(\+(\d+))" )
m = p.search ( arguments )
print type ( m )
m_list = list ( m.groups () )
print type ( m_list )
from1 = str ( m_list[0] )
to1 = "-k " + str ( int ( m_list[1] ) + 1 )
cmd1 = str ( arguments.replace ( from1, to1 ) )
print cmd1
os.system ( cmd1 )

Now, this is what I get (on three different machines with different
versions of python):
<type '_sre.SRE_Match'>
<type 'list'>
./sort -F -k 2 -e
<type 'NoneType'>
Traceback (most recent call last):
  File "./sort", line 9, in <module>
    m_list = list ( m.groups () )
AttributeError: 'NoneType' object has no attribute 'groups'

Please note the unrequested output of '<type 'NoneType'>'. The strange
thing about this all is the fact that  the whole thing works as
expected when typed  into the interpreter. I would be glad if anyone
could help.

Best regards,
Kay



More information about the Python-list mailing list