Search code examples
pythonjupyter-notebooksubprocessgdal

subprocess doesn't write the output file


I'm working with python in a jupyter notebook.

I want to execute the following command :

$ gdalbuildvrt tmp_merge files

with tmp_merge being an output file of the function and set to : /home/prambaud/gfc_results/test/tmp_tile.vrt
and files being all the tiles to merge in the vrt file set to :/home/prambaud/gfc_results/test/tile_*.tif

This function authorize the use of the wildcard.

To run it in my Jupyter notebook I use the subprocess module:

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)

as a result I obtain the following :

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

with no error messages. BUT the output file is not created. Does anyone know what could prevent the subprocess.run function to create and write in a file ?

PS:
I've also tried to run the command from the jupyter notebook with ! and the same parameters and the tmp file have of course bee created...


Solution

  • My command can only be executed from shell so using subprocess I need to add the shell keyword as :

    process = subprocess.run(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        universal_newlines=True,
        shell=True
    )