问题描述
限时送ChatGPT账号..我对 Python 完全陌生.
I am completely new to python.
我正在尝试使用用户界面传递输入和输出的位置,如本特定讨论 [1] 所示:如何给出输入"的位置和输出"对于使用用户界面的 Python 代码并从 UI 本身运行代码?
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?
但是在这里,我正在调用一个外部命令,并尝试通过传递输入和输出的位置来从我的 python 代码中运行它,如上述情况.
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
我做错了什么?
推荐答案
您的 cmd
不正确.
用值连接字符串
cmd = "gpt F:\saikiran\myGraph.xml -Psource=" + input_file + " - Ptarget=" + intermediate_file
或使用字符串格式
cmd = "gpt F:\saikiran\myGraph.xml -Psource={} - Ptarget={}".format(input_file, intermediate_file)
在 Python 3.6 或 3.7 中你可以使用 f-string
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}"
<小时>
当前cmd
"gpt F:\saikiran\myGraph.xml -Psource=input_file - Ptarget=intermediate_file"
将创建名称为字面意思的文件
will create file with name literally
intermediate_file
不是
F:/saikiran/ddd
它可以在 gdal.Open()
这篇关于无法使用 python 中的用户界面将输入和输出的位置传递给外部命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论