在python脚本中调用位于path_to_python_environment/Scripts
的另一个脚本的最佳方法是什么,例如path_to_python_environment/Scripts/gdal_calc.py
?
非工作解决方案:
1)
import gdal_calc
gdal_calc('--help')
2)
import subprocess
subprocess.Popen('python gdal_calc.py --help')
工作解决方案,但必须有一个更简单的方法:
import subprocess
path_to_python_environment = os.path.dirname(sys.executable)
path_to_script = os.path.join(path_to_python_environment, 'Scripts', 'gdal_calc.py')
subprocess.Popen('python ' + path_to_script + ' --help')
答案 0 :(得分:0)
您始终可以从python脚本启动它,而无需使用shell脚本:
# import section
import os
# Manual input
a_source = "aaa.tif"
b_source = "bbb.tif"
output = "out.tif"
calc = '"(A**B)*(B==1)"'
# The command itself
gdal_calc = 'python {Path root}/Lib/site-packages/osgeo/scripts/gdal_calc.py ' \
'-A {0} ' \
'-B {1} ' \
'--outfile={2} ' \
'--calc={3} ' \
'--type=Float32 ' \
'--overwrite'.format(a_source, b_source, output, calc)
# Launch the command
os.system(gdal_calc)
我发现这更加容易和清楚。