Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何制作一个可执行的shell脚本来运行my.py程序并输入文件_Python_Bash_Shell_Terminal_Stdin - Fatal编程技术网

Python 如何制作一个可执行的shell脚本来运行my.py程序并输入文件

Python 如何制作一个可执行的shell脚本来运行my.py程序并输入文件,python,bash,shell,terminal,stdin,Python,Bash,Shell,Terminal,Stdin,我有一个python程序,它从std输入中获取一个文件作为输入,对数据运行一些计算,并将结果输出到std输出。以下是我用于获取输入文件的代码: import math import sys name = sys.argv[-1] inf = open(name, 'r') outf = open("test.txt", 'w') col1, col2, col3 = [], [], [] for row in inf: a, b, c = row.split('\t') c

我有一个python程序,它从std输入中获取一个文件作为输入,对数据运行一些计算,并将结果输出到std输出。以下是我用于获取输入文件的代码:

import math
import sys

name = sys.argv[-1]
inf = open(name, 'r') 
outf = open("test.txt", 'w')

col1, col2, col3 = [], [], []
for row in inf:
    a, b, c = row.split('\t')
    col1.append(a)
    col2.append(b)
    col3.append(c)
我需要创建一个shell脚本来执行程序并提供输入文件。理想情况下,shell脚本应该能够正常工作,以便按如下方式使用:

$ cat inputfile.txt | scriptname
和/或

$ ./scriptname < inputfile
$。/scriptname
我已经尝试了几个不同的基本shell脚本,但没有任何运气让它工作。如果您能就如何编写此脚本和/或如何修改python代码以使其正常工作提出建议,我将不胜感激。谢谢

如果有帮助的话,我相信我正在MacOSX10.8上使用python 2.7.3


另外,这实际上是我第一次体验编程(我开始学习python只是为了这个项目),所以请原谅这些糟糕的代码。

像这样执行脚本:

./shellscript.sh inputfile.txt
在shellscript.sh中:

python scriptname.py $1

确保python脚本和输入文件与shell脚本位于同一路径中,或者修改脚本以反映python脚本的位置
/path/to/script/scriptname.py

向脚本添加一个“shebang”,使用chmod使其可执行,并且运行时不使用任何其他包装。您的代码从命令行打开文件,命令行不是stdin或stdout,因此必须更改。既然你什么都不写,我就编了些东西

但应该是这样的:

脚本名称:

#!/usr/bin/env python

import math
import sys

col1, col2, col3 = [], [], []
for row in sys.stdin:
    a, b, c = row.split('\t')
    col1.append(a)
    col2.append(b)
    col3.append(c)
sys.stdout.write('something should be written\n')
在贝壳里

$ chmod u+x scriptname
$ cat inputfile.txt | ./scriptname
something should be written
$ ./scriptname < inputfile.txt
something should be written
$chmod u+x scriptname
$cat inputfile.txt |./scriptname
应该写点什么
$./scriptname
首先,您的代码没有从标准文本读取;它正在以最后一个参数指定的路径读取文件。这意味着shell脚本不可能为其提供输入*

您希望更改代码,使其始终从stdin读取,或者如果未指定文件名,则从stdin读取,或者如果指定
-
作为文件名,则从stdin读取。例如:

if len(sys.argv) > 1:
    name = sys.argv[-1]
    inf = open(name, 'r')
except:
    inf = sys.stdin

最重要的是,您的脚本无法运行。在Windows上,您需要使用扩展名
.py
来命名它。但在几乎任何其他平台上,您都需要
chmod+xscriptname
文件以使其可运行,并在脚本顶部添加一行shebang,告诉计算机使用哪个解释器来运行它(因为否则它会猜测
/bin/sh
或其他无用的东西)。特别是,在任何类似POSIX的现代系统(包括OS X和linux)上,您可以执行以下操作:

#!/usr/bin/env python

解决了这两个问题后,您现在可以完全按照自己的意愿进行操作:

$ ./scriptname < inputfile

但是,您可能需要考虑使用该模块,它神奇地为您提供了这方面的大部分:

import fileinput
inf = fileinput.input()


*好的,您可以将输入保存到一个临时文件中,然后将该路径提供给它。但是这是欺骗。

如果您希望应用程序同时处理这两个问题,即能够传递文件,并且能够在内部将stdin视为完全相同的文件对象,那么您应该研究StringIO/cStringIO:

为什么不直接从命令行用python yourpythonprogram.py inputfile.txt执行python脚本呢?谢谢。这对我来说很好,但这是学校作业,教授想通过在可执行的shell脚本上运行上述命令来测试我的程序。别担心,这不是编程课,所以我没有作弊:)对不起,这真的很乱。只要让python脚本可执行并放弃额外的shell脚本。当然我同意你的观点,但我们不知道他为什么这样做,所以这回答了他的问题。“假设这是linux”不是一个很好的假设,因为问题是“我相信我正在使用……mac osx 10.8”。但是这对于任何现代的POSIX系统来说都是正确的,所以只要去掉前三个词,一切都很好。
import fileinput
inf = fileinput.input()