rajan
November 4th, 2010, 09:00 PM
I am having problems putting either a sed or an awk command in my python script.
Using this script:
import sys
import os
def splitter(y):
fop = open(y,'w')
fop.write('hello world\n')
return fop
filehandl = splitter(sys.argv[1])
filehandl.write('now got here\n')
string1 = 'awk \'{ sub(/got here/,\"hot gere\"); print }\' '+filehandl.name+' > '+filehandl.name+'_2'
os.system(string1)
print string1
os.system('cat '+filehandl.name+'_2')
user@mirror15 [~/ClassicalMech/SphereSim] % python este.py atre
awk '{ sub(/got here/,"hot gere"); print }' atre > atre_2
This clearly isn't the way things should work. Instead of replacing the regexp in the file "atre" and putting the output into "atre_2", "atre_2" is simply an empty file. I will also note that this same thing happens with an equivalent implementation of sed.
The really confusing part is that when I print the command that python feeds the command line in the line:
os.system(string1)
and then I copy that printed string1 directly into my terminal, it does things as expected.
The gist of this problem is that the same command does different things on the command line versus in the context of a python script. Even when I enter the python interpreter, I can do everything normally and everything works. The python script just doesn't want to work. Any ideas?
Using this script:
import sys
import os
def splitter(y):
fop = open(y,'w')
fop.write('hello world\n')
return fop
filehandl = splitter(sys.argv[1])
filehandl.write('now got here\n')
string1 = 'awk \'{ sub(/got here/,\"hot gere\"); print }\' '+filehandl.name+' > '+filehandl.name+'_2'
os.system(string1)
print string1
os.system('cat '+filehandl.name+'_2')
user@mirror15 [~/ClassicalMech/SphereSim] % python este.py atre
awk '{ sub(/got here/,"hot gere"); print }' atre > atre_2
This clearly isn't the way things should work. Instead of replacing the regexp in the file "atre" and putting the output into "atre_2", "atre_2" is simply an empty file. I will also note that this same thing happens with an equivalent implementation of sed.
The really confusing part is that when I print the command that python feeds the command line in the line:
os.system(string1)
and then I copy that printed string1 directly into my terminal, it does things as expected.
The gist of this problem is that the same command does different things on the command line versus in the context of a python script. Even when I enter the python interpreter, I can do everything normally and everything works. The python script just doesn't want to work. Any ideas?