2017-09-05 5 views
0
[[email protected] ~]# python 
Python 2.7.5 (default, Nov 6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import os 
>>> os.system("source /home/oracle/.bash_profile") 
0 
>>> os.system("echo $PATH") 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin 
0 
>>> 

なぜPATHが変更されませんか? what the 0意味なぜos.system( "source /home/oracle/.bash_profile")が動作しないのですか

+0

使用 'os.popen()'(サブプロセスがその領域であなたを助けます)、良いアイデアであるとパスを、あなたはglob'モジュール – Gahan

答えて

2

os.systemはシェルを生成しますので、自分のパスをsourceコマンドで設定してすぐに終了します。あなたは、パスを設定することができ、その後すぐにプログラムを実行します。私はあなたを警告するでしょうけれども

os.system("source /home/oracle/.bash_profile && your_command_here") 

を、これは外部のプログラムを実行するための推奨方法ではありません。 subprocessを使用すると、産卵の殻を避けているよう

https://docs.python.org/2/library/subprocess.html#module-subprocess

+1

あなたのanwserをthankes '使用することができます –

関連する問題