无法使用 python 中的用户界面将输入和输出的位置传递给外部命令

编程入门 行业动态 更新时间:2024-10-27 14:22:22
本文介绍了无法使用 python 中的用户界面将输入和输出的位置传递给外部命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-30 06:26:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390819.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户界面   命令   位置   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!