I am completely new to python.

I am trying to pass the location of Input and output using User interface as shown in this particular discussion [1]:How to give the location of "Input" and "Output" for a python code using User Interface and run the code from UI itself?

But here, I am calling an external command and trying to run it from my python code by passing location of input and output as in the above mentioned case.

from tkinter import *
from tkinter import filedialog

import numpy as np
import gdal
gdal.UseExceptions()
import os
def your_code(input_file, intermediate_file, output_file):

    cmd = "gpt F:\saikiran\myGraph.xml -Psource=input_file - Ptarget=intermediate_file"
    os.system(cmd)
    ds = gdal.Open(intermediate_file)
    band = ds.GetRasterBand(1)
……………………………………………...
#gen_map_button.place(x=230, y=300)
gen_map_button.pack()

root.mainloop()

But I encountered with this error :

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\User\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
  File "C:\Users\User\GUI-pywt.py", line 145, in gen_map
your_code(input_filename, intermediate_filename, output_filename)
  File "C:\Users\User\GUI-pywt.py", line 15, in your_code
ds = gdal.Open(intermediate_file)
  File "C:\Users\User\Anaconda3\lib\site-packages\osgeo\gdal.py", line 3251, in Open
return _gdal.Open(*args)
RuntimeError: F:/saikiran/ddd: No such file or directory

What mistake did i do ?

1 Answers

0
furas On Best Solutions

Your cmd is not correct.

Concatenate string with values

cmd = "gpt F:\saikiran\myGraph.xml -Psource=" + input_file + " - Ptarget=" + intermediate_file 

or use string formatting

cmd = "gpt F:\saikiran\myGraph.xml -Psource={} - Ptarget={}".format(input_file, intermediate_file) 

With Python 3.6 or 3.7 you can use f-string

cmd = f"gpt F:\saikiran\myGraph.xml -Psource={input_file} - Ptarget={intermediate_file}" 

Current cmd

"gpt F:\saikiran\myGraph.xml -Psource=input_file - Ptarget=intermediate_file"

will create file with name literally

 intermediate_file

not

 F:/saikiran/ddd

and it can make problem in gdal.Open()