3中调用命令行参数

3中调用命令行参数

本文介绍了在Python 3中调用命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python 3编写一个程序,该程序将从csv文件中获取数据,对其进行调整,然后将新数据写入将由用户命名的csv文件中。

I am writing a program in Python 3 that will take data from a csv file, make adjustments to it and write the new data to a csv file that will be named by the user.

我只是想了解命令行部分。
我希望它以这种格式正确执行。

I'm just trying to understand the command line part.I want it to execute properly in this format.

$ program.py input.csv output.csv

我计划还要检查是否有3个参数。

I plan to also make it check that there are 3 total arguments.

我的问题是,在命令行上输入的文件名在哪里?
是否保存到默认变量?如何在python程序中调用这些文件名?

My question is where do this filename inputs on the command line go?Are they saved to a default variable? How can I call these filenames within my python program?

推荐答案

命令行选项存储在sys.argv中。您可能需要检查以下内容:

Command line options are stored in sys.argv. You may want to check this:

此外,如果您需要更复杂的命令行参数解析并基于此来决定程序逻辑,则应尝试

Beside, if you need more complex command-line argument parsing and deciding your program logic based on that, you should try argparse

这篇关于在Python 3中调用命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:22