如何在 python 中输入文件并对其运行异步命令?
我正在尝试编写一个脚本,要求输入文件,然后对其运行一些命令。当我运行脚本时,它会要求我提供文件名,当我给出文件(例如 example.bam)时,我会收到此错误:
NameError:名称“example.bam”未定义
我尝试了很多方法,但无法修复它。有人能告诉我出了什么问题吗?
这是我的命令:
from subprocess import call
filename = input ("filename: ");
with open (filename, "r") as a:
for command in ("samtools tview 'a' /chicken/chick_build2.1_unmasked.fa",):
call(command, shell=True)
这是我的命令的简短版本:它必须做更多的事情。我还想同时输入4-6个文件(也许这个信息有助于澄清我的意图)。
I'm trying to write a script that asks for an input file and then runs some command on it. when I run the script it askes me for filename and when I give the file (e.g example.bam) then I get this error:
NameError: name 'example.bam' is not defined
I tried many things but I couldn't fix it. Can someone tell me what is wrong?
This is my comand:
from subprocess import call
filename = input ("filename: ");
with open (filename, "r") as a:
for command in ("samtools tview 'a' /chicken/chick_build2.1_unmasked.fa",):
call(command, shell=True)
This is a short version of my command: it has to do much more stuff. I'm also thinking to input 4-6 files at same time (perhaps this information is helpful to clarify my intentions).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
input
相当于eval(raw_input(prompt))
。因此,您的脚本当前尝试执行的操作是解释您的输入(在您的情况下为“示例”),并像脚本中的语句一样执行。对于用户输入(我可以简单地说“对于任何输入”——除非您知道自己在做什么),请始终使用raw_input
函数。因此,要解决这个问题,请将
input
替换为raw_input
:input
is equivalent toeval(raw_input(prompt))
. So what your script currently tries to do is interpret your input ("example", in your case), and execute as if it were a statement in your script. For user input (and might I simply say "for any input" -- unless you know what you're doing), always use theraw_input
function.So, to solve it, replace
input
withraw_input
: