如何从终端为python脚本获取输入文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20157824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to take input file from terminal for python script?
提问by Hell Man
I have a python script which uses a text file and manipulate the data from the file and output to another file. Basically I want it to work for any text file input. Right now I readline from the file and then print the output to screen. I want the output in a file.
我有一个 python 脚本,它使用文本文件并操作文件中的数据并输出到另一个文件。基本上我希望它适用于任何文本文件输入。现在我从文件中读取行,然后将输出打印到屏幕上。我想要一个文件中的输出。
So user can type the following and test for any file:
因此用户可以键入以下内容并测试任何文件:
cat input_file.txt | python script.py > output_file.txt.
How can I implement this in my script? Thank You.
如何在我的脚本中实现它?谢谢你。
cat is command in linux. I don't know how it works.
cat 是 Linux 中的命令。我不知道它是如何工作的。
采纳答案by Kyle Neary
The best way to do this is probably to call the input and output files as arguments for the python script:
最好的方法可能是调用输入和输出文件作为 python 脚本的参数:
import sys
inFile = sys.argv[1]
outFile = sys.argv[2]
Then you can read in all your data, do your manipulations, and write out the results:
然后,您可以读入所有数据,进行操作并写出结果:
with open(inFile,'r') as i:
lines = i.readlines()
processedLines = manipulateData(lines)
with open(outFile,'w') as o:
for line in processedLines:
o.write(line)
You can call this program by running python script.py input_file.txt output_file.txt
你可以通过运行来调用这个程序 python script.py input_file.txt output_file.txt
If you absolutely must pipe the data to python (which is really not recommended), use sys.stdin.readlines()
如果您绝对必须将数据通过管道传输到 python(真的不推荐),请使用 sys.stdin.readlines()
回答by JoeC
cat input_file.txt | python script.py > output_file.txt.
You can passing a big string that has all the data inside input_file.txt instead of an actual file so in order to implement your python script, just take that it as a string argument and split the strings by new line characters, for example you can use "\n" as a delimiter to split that big string and to write to an outputfile, just do it in the normal way
您可以传递一个包含 input_file.txt 中所有数据的大字符串而不是实际文件,因此为了实现您的 Python 脚本,只需将其作为字符串参数并按换行符拆分字符串,例如您可以使用“\n”作为分隔符来分割大字符串并写入输出文件,只需以正常方式进行
i.e. open file, write to the fileand close file
即open file,write to the file和close file
回答by abarnert
Sending output to a file is very similar to taking input from a file.
将输出发送到文件与从文件中获取输入非常相似。
You open a file for writing the same way you do for reading, except with a 'w'mode instead of an 'r'mode.
您打开文件以与读取相同的方式进行写入,除了使用'w'模式而不是'r'模式。
You write to a file by calling writeon it the same way you read by calling reador readline.
您可以通过调用文件来写入文件,write就像调用read或读取文件一样readline。
This is all explained in the Reading and Writing Filessection of the tutorial.
这一切都在本教程的读取和写入文件部分进行了解释。
So, if your existing code looks like this:
因此,如果您现有的代码如下所示:
with open('input.txt', 'r') as f:
while True:
line = f.readline()
if not line:
break
print(line)
You just need to do this:
你只需要这样做:
with open('input.txt', 'r') as fin, open('output.txt', 'w') as fout:
while True:
line = fin.readline()
if not line:
break
fout.write(line)
If you're looking to allow the user to pass the filenames on the command line, use sys.argvto get the filenames, or use argparsefor more complicated command-line argument parsing.
如果您希望允许用户在命令行上传递文件名,请使用sys.argv获取文件名,或argparse用于更复杂的命令行参数解析。
For example, you can change the first line to this:
例如,您可以将第一行更改为:
import sys
with open(sys.argv[1], 'r') as fin, open(sys.argv[2], 'w') as fout:
Now, you can run the program like this:
现在,您可以像这样运行程序:
python script.py input_file.txt outputfile.txt
回答by Sticky
This method (your question) describes reading data from STDIN:
此方法(您的问题)描述了从 STDIN 读取数据:
cat input_file.txt | python script.py
cat input_file.txt | python script.py
Solution: script.py:
解决方案script.py::
import sys
for line in sys.stdin:
print line
The method in above solutions describes taking argument parameters with your python call:
上述解决方案中的方法描述了使用 python 调用获取参数参数:
python script.py input_file.txt
python script.py input_file.txt
Solution: script.py:
解决方案script.py::
import sys
with open(sys.argv[1], 'r') as file:
for line in file:
print line
Hope this helps!
希望这可以帮助!
回答by dkula
cat input_file.txt | python script.py > output_file.txt
Basically, python script needs to read the input file and write to the standard output.
基本上,python 脚本需要读取输入文件并写入标准输出。
import sys
with open('input_file.txt', 'r') as f:
while True:
line = f.readline()
if not line:
break
sys.stdout.write(line)

