如何从命令行执行我的Python脚本和输入文件? [英] How do I execute my Python script and input file from command line?

查看:160
本文介绍了如何从命令行执行我的Python脚本和输入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件。但我不想直接在我的python脚本中输入该文件。我想从命令行运行我的python脚本,参数是我的文本文件。我想要的命令行是:$ Python.exe script.py C:/user/text_file.txt。



我试过的:



I have a text file. But I dont want to input that file in my python script directly. I want to run my python script from command line with argument which is my text file. The command line that I want is : $Python.exe script.py C:/user/text_file.txt .

What I have tried:

<pre>
import sys

def main():
    infile = sys.argv[0]
    Array = ["f1", "f2", "f3", "f4"]
    with open(infile, "r") as input_file:
    	output_list = []
    	for rec in input_file.read().splitlines():
    		rec = rec[:-3]
    		FBlist = [rec[i:i+2] for i in range(0, len(rec), 2)]
    		output_list.append(FBlist)
    		print(output_list)



main()

推荐答案

Python。 exe script.py C:/user/text_file.txt。



我尝试过:



Python.exe script.py C:/user/text_file.txt .

What I have tried:

<pre>
import sys

def main():
    infile = sys.argv[0]
    Array = ["f1", "f2", "f3", "f4"]
    with open(infile, "r") as input_file:
    	output_list = []
    	for rec in input_file.read().splitlines():
    		rec = rec[:-3]
    		FBlist = [rec[i:i+2] for i in range(0, len(rec), 2)]
    		output_list.append(FBlist)
    		print(output_list)



main()


我昨天解释了如何做到这一点您在 https:// www的问题.codeproject.com / Questions / 1279057 /如何读取外部路径与Python-co的文件[ ^ ]。如果这不起作用,那么使用原始问题发表评论。请不要重新打开相同的查询。
I explained how to do this yesterday in your question at https://www.codeproject.com/Questions/1279057/How-to-read-a-file-in-external-path-with-Python-co[^]. If that does not work then use the original question to post comments. Please do not reopen the same query.


这篇关于如何从命令行执行我的Python脚本和输入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆