【发布时间】:2013-07-02 17:01:20
【问题描述】:
所以我在 Ubuntu 12.04 上运行了一个功能齐全的 py 脚本,一切正常。除了我不喜欢我的输入法之外,它会变得很烦人,如下所示。在我输入代码之前,我应该说代码以 .img 格式获取两个图像,然后对它们进行计算。这是我所拥有的:
import os
first = raw_input("Full path to first .img file: ")
second = raw_input("Full path to second .img file: ")
print " "
if os.path.exists(first) == True:
if first.endswith('.img') == False:
print 'You did not give a .img file, try running again'
os.sys.exit()
elif os.path.exists(second) == True:
if second.endswith('.img') == False:
print 'You did not give a .img file, try running again'
os.sys.exit()
else:
print "Your path does not exist, probably a typo. Try again"
os.sys.exit()
这就是我想要的;我希望能够直接从终端向 python 提供此输入。换句话说,我希望能够在终端中输入类似
python myscript.py with the two images as input
这样我可以在指定路径和内容时使用终端的制表键快捷方式。有什么想法/建议吗?
编辑:好的,所以我研究了解析,我想我知道如何使用它。这是我的代码:
import argparse
import nipy
parser = argparse.ArgumentParser()
parser.add_argument("-im", "--image_input", help = "Feed the program an image", type = nipy.core.image.image.Image, nargs = 2)
但是现在我希望能够在脚本中使用这些文件,方法是说 first = parser[0] second = parse[1] 并在 first 和 second 上执行操作。这可以实现吗?
【问题讨论】:
标签: python linux python-2.7