0

Merge TIFF files using GDAL. Output file was not created. How can I solve it?

Code

import subprocess
import glob
import os
import shlex

file_list = glob.glob(r"E:\Spekter\data\data\Task2\*.tif")
files_string = " ".join(file_list)
output_path = r"E:\Spekter\Task 2\output.tif"
command = f"gdal_merge.py -o {output_path} -of gtiff {files_string}"

print("Command:", command)
os.system(command)

if os.path.exists(output_path):
  print(f"Output file '{output_path}' created successfully.")
else:
  print("Output file was not created. Check for errors and permissions.")

Output

Command: gdal_merge.py -o E:\Spekter\Task 2\output.tif -of gtiff E:\Spekter\data\data\Task2\618_5405.tif E:\Spekter\data\data\Task2\618_5406.tif E:\Spekter\data\data\Task2\618_5407.tif E:\Spekter\data\data\Task2\619_5403.tif E:\Spekter\data\data\Task2\619_5404.tif E:\Spekter\data\data\Task2\619_5405.tif E:\Spekter\data\data\Task2\619_5406.tif E:\Spekter\data\data\Task2\619_5407.tif E:\Spekter\data\data\Task2\619_5408.tif E:\Spekter\data\data\Task2\619_5409.tif E:\Spekter\data\data\Task2\620_5400.tif E:\Spekter\data\data\Task2\620_5401.tif E:\Spekter\data\data\Task2\620_5402.tif E:\Spekter\data\data\Task2\620_5403.tif E:\Spekter\data\data\Task2\620_5404.tif E:\Spekter\data\data\Task2\620_5405.tif E:\Spekter\data\data\Task2\620_5406.tif E:\Spekter\data\data\Task2\620_5407.tif E:\Spekter\data\data\Task2\620_5408.tif E:\Spekter\data\data\Task2\620_5409.tif
Output file was not created. Check for errors and permissions.
5
  • There is a space in output file path. Try to quote the output argument: command = f'gdal_merge.py -o "{output_path}" -of gtiff {files_string}'
    – Dataform
    Commented Oct 23, 2023 at 18:33
  • I would first try with a path that has no space in the name Task 2 -> Task2.
    – user30184
    Commented Oct 23, 2023 at 18:34
  • The following code not worked. import subprocess import glob import os import shlex file_list = glob.glob(r"E:\Spekter\data\data\Task2*.tif") files_string = " ".join(file_list) output_path = r"E:\Spekter\Task2\output.tif" command = command = f'gdal_merge.py -o "{output_path}" -of gtiff {files_string}' print("Command:", command) os.system(command) if os.path.exists(output_path): print(f"Output file '{output_path}' created successfully.") else: print("Output file was not created. Check for errors and permissions." Commented Oct 24, 2023 at 4:29
  • Have you tried to run the command directly in a shell? May be you get some output that can help you. You can also add -v to get more output.
    – Dataform
    Commented Oct 24, 2023 at 5:34
  • I have run the basic command in a shell. But I have to write in python. I am a beginner in GDAL. Can you help me write the full code. My task it that Merge TIFF files using GDAL. Scenario: Create a tool that takes a folder path as input, and merges all the .tif files within a folder into a single TIFF file using GDAL. Input Folder Location Output Merged TIFF file Commented Oct 24, 2023 at 6:14

1 Answer 1

1

I modified your code slightly

import subprocess
import glob
import os
import shlex

file_list = glob.glob(r"C:\data\dem\P*.tif")
files_string = " ".join(file_list)
output_path = r"C:\data\dem\output.tif"
command = f"gdal_merge.py -o {output_path} -of gtiff {files_string}"

print("Command:", command)
os.system(command)

if os.path.exists(output_path):
  print(f"Output file '{output_path}' created successfully.")
else:
  print("Output file was not created. Check for errors and permissions.")

Then I executed it successfully:

python mergetest.py
Command: gdal_merge.py -o C:\data\dem\output.tif -of gtiff C:\data\dem\Ptest.tif
0...10...20...30...40...50...60...70...80...90...100 - done.
Output file 'C:\data\dem\output.tif' created successfully.

It worked also with a longer {files_string}. Your code works, check again the input and output strings.

2
  • Not Wokred. I don't know where is the problem. Please suggest how I can I solve. import subprocess import glob import os import shlex file_list = glob.glob(r"E:\Spekter\data\data\Task2*.tif") files_string = " ".join(file_list) output_path = r"E:\Spekter\Task2\output.tif" command = f"gdal_merge.py -o {output_path} -of gtiff {files_string}" print("Command:", command) os.system(command) if os.path.exists(output_path): print(f"Output file '{output_path}' created successfully.") else: print("Output file was not created. Check for errors and permissions.") Commented Oct 24, 2023 at 7:01
  • I can only suggest to make a test with a single tiff file as an input first. Maybe some of the tiff files in the list is corrupted or maybe the command line has too many characters or something. Generally speaking gdalbuildvrt gdal.org/programs/gdalbuildvrt.html is better than gdal_merge for most use cases.
    – user30184
    Commented Oct 24, 2023 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.