In python 2.4, how can I execute external commands

2019-03-31 02:40发布

Without using the new 2.6 subprocess module, how can I get either os.popen or os.system to execute my commands using the tcsh instead of bash? I need to source some scripts which are written in tcsh before executing some other commands and I need to do this within python2.4.

EDIT Thanks for answers using 'tcsh -c', but I'd like to avoid this because I have to do escape madness. The string will be interpreted by bash and then interpreted by tcsh. I'll have to do something like:

os.system("tcsh -c '"+re.compile("'").sub(r"""'"'"'""",my_cmd)+"'")

Can't I just tell python to open up a 'tcsh' sub-process instead of a 'bash' subprocess? Is that possible?

P.S. I realize that bash is the cat's meow, but I'm working in a corporate environment and I'm going to choose to not fight a tcsh vs bash battle -- bigger fish to fry.

3条回答
戒情不戒烟
2楼-- · 2019-03-31 03:11

Just set the shell to use to be tcsh:

>>> os.environ['SHELL'] = 'tcsh'
>>> os.environ['SHELL']
'tcsh'
>>> os.system("echo $SHELL")
tcsh
查看更多
The star\"
3楼-- · 2019-03-31 03:16

How about:

>>> os.system("tcsh your_own_script")

Or just write the script and add

#!/bin/tcsh

at the beginning of the file and let the OS take care of that.

查看更多
等我变得足够好
4楼-- · 2019-03-31 03:25

Just prefix the shell as part of your command. I don't have tcsh installed but with zsh:

>>> os.system ("zsh -c 'echo $0'")
zsh
0
查看更多
登录 后发表回答