子进程未写入输出文件

时间:2020-07-16 11:40:49

标签: python jupyter-notebook subprocess gdal

我正在Jupyter笔记本中使用python。

我要执行以下命令:

$ gdalbuildvrt tmp_merge files

,其中tmp_merge是函数的输出文件,并设置为:/home/prambaud/gfc_results/test/tmp_tile.vrt
并且files是要合并到vrt文件中并设置为:/home/prambaud/gfc_results/test/tile_*.tif

的所有图块

此功能授权使用通配符。

要在Jupyter笔记本中运行它,请使用subprocess模块:

command = [
    'gdalbuildvrt',
    '/home/prambaud/gfc_results/test/tmp_tile.vrt',
    '/home/prambaud/gfc_results/test/tile_*.tif'
]

process = subprocess.run(
    command,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    universal_newlines=True,
    #cwd=os.path.expanduser('~')
)
    
print(process. stdout)

结果,我得到以下信息:

0...10...20...30...40...50...60...70...80...90...100 - done.

,无错误消息。但是未创建输出文件。有谁知道阻止subprocess.run函数创建和写入文件的原因吗?

PS:
我还尝试过使用!从jupyter笔记本中运行命令,并且当然会创建相同的参数和tmp文件……

1 个答案:

答案 0 :(得分:0)

我的命令只能从shell执行,因此在使用子进程时,我需要将shell关键字添加为:

process = subprocess.run(
    command,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    universal_newlines=True,
    shell=True
)
相关问题