strange behavior with os.system

K

kmw

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
 
M

MRAB

kmw said:
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

Shorter:

arguments = " ".join(sys.argv)
p = re.compile ( "(\+(\d+))" )

This looks for a "+" followed by digits.
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

No "+"s, so the p.search(arguments) returns None.
 
P

Piet van Oostrum

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

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

MRAB has already given you some insight, I hope.
But are you aware that you are calling your own program again?
Or did you want to call the standard sort program? In that case you
shouldn't call ./sort as it is in argv[0], but just sort (assuming '.'
is not in your PATH) or the full path, like /usr/bin/sort.
 
K

kmw

That's it. I am calling my own program and not coreutils' sort, what
explains the unrequested output. Many thanks.

Cheers,
Kay

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

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

MRAB has already given you some insight, I hope.
But are you aware that you are calling your own program again?
Or did you want to call the standard sort program? In that case you
shouldn't call ./sort as it is in argv[0], but just sort (assuming '.'
is not in your PATH) or the full path, like /usr/bin/sort.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top