Python从命令行指定配置文件

Python从命令行指定配置文件,python,command-line,command,config,Python,Command Line,Command,Config,我已经创建了一个python程序,我将编译成EXE,但在此之前,我希望有一个命令行开关,让我指定要使用的配置文件。我的想法是使用多个配置文件来满足不同的目的。我在网上搜索过,但我对所看到的东西感到困惑。任何关于如何做的建议都将不胜感激 您应该看看这个模块,它为您处理命令行选项 编辑:让我给你一个简单的例子 import argparse # create a new argument parser parser = argparse.ArgumentParser(description="Si

我已经创建了一个python程序,我将编译成EXE,但在此之前,我希望有一个命令行开关,让我指定要使用的配置文件。我的想法是使用多个配置文件来满足不同的目的。我在网上搜索过,但我对所看到的东西感到困惑。任何关于如何做的建议都将不胜感激

您应该看看这个模块,它为您处理命令行选项

编辑:让我给你一个简单的例子

import argparse

# create a new argument parser
parser = argparse.ArgumentParser(description="Simple argument parser")
# add a new command line option, call it '-c' and set its destination to 'config'
parser.add_argument("-c", action="store", dest="config_file")

# get the result
result = parser.parse_args()
# since we set 'config_file' as destination for the argument -c, 
# we can fetch its value like this (and print it, for example):
print(result.config_file)

然后,您可以继续使用
result.config_file
作为传递给脚本的配置文件的文件名。

您应该看看模块,它为您处理命令行选项

编辑:让我给你一个简单的例子

import argparse

# create a new argument parser
parser = argparse.ArgumentParser(description="Simple argument parser")
# add a new command line option, call it '-c' and set its destination to 'config'
parser.add_argument("-c", action="store", dest="config_file")

# get the result
result = parser.parse_args()
# since we set 'config_file' as destination for the argument -c, 
# we can fetch its value like this (and print it, for example):
print(result.config_file)

然后,您可以继续使用
result.config_file
作为传递给脚本的配置文件的文件名。

您应该看看模块,它为您处理命令行选项

编辑:让我给你一个简单的例子

import argparse

# create a new argument parser
parser = argparse.ArgumentParser(description="Simple argument parser")
# add a new command line option, call it '-c' and set its destination to 'config'
parser.add_argument("-c", action="store", dest="config_file")

# get the result
result = parser.parse_args()
# since we set 'config_file' as destination for the argument -c, 
# we can fetch its value like this (and print it, for example):
print(result.config_file)

然后,您可以继续使用
result.config_file
作为传递给脚本的配置文件的文件名。

您应该看看模块,它为您处理命令行选项

编辑:让我给你一个简单的例子

import argparse

# create a new argument parser
parser = argparse.ArgumentParser(description="Simple argument parser")
# add a new command line option, call it '-c' and set its destination to 'config'
parser.add_argument("-c", action="store", dest="config_file")

# get the result
result = parser.parse_args()
# since we set 'config_file' as destination for the argument -c, 
# we can fetch its value like this (and print it, for example):
print(result.config_file)

然后,您可以继续使用
result.config_file
作为传递给脚本的配置文件的文件名。

看一看。为什么不为配置文件使用命令行参数?看一看。为什么不为配置文件使用命令行参数?看一看。为什么不为脚本使用命令行参数配置文件?看一下。为什么不在配置文件中使用命令行参数呢?谢谢,我在看这个,但我在这方面是个新手,我对我看到的有点困惑。。。如果您能提供任何额外的见解,我们将不胜感激。。。我将继续尝试找出答案…因此,如果我理解我看到的内容,我可以导入argparse parser=argparse.ArgumentParser.add_argument('-c',action=“store”,dest=“config”)config=ConfigParser.ConfigParser()config.readfp(open(config))user3739525:试试看。谢谢,我在看,但我是一个巨大的新手在这方面,我有点困惑,我所看到的。。。如果您能提供任何额外的见解,我们将不胜感激。。。我将继续尝试找出答案…因此,如果我理解我看到的内容,我可以导入argparse parser=argparse.ArgumentParser.add_argument('-c',action=“store”,dest=“config”)config=ConfigParser.ConfigParser()config.readfp(open(config))user3739525:试试看。谢谢,我在看,但我是一个巨大的新手在这方面,我有点困惑,我所看到的。。。如果您能提供任何额外的见解,我们将不胜感激。。。我将继续尝试找出答案…因此,如果我理解我看到的内容,我可以导入argparse parser=argparse.ArgumentParser.add_argument('-c',action=“store”,dest=“config”)config=ConfigParser.ConfigParser()config.readfp(open(config))user3739525:试试看。谢谢,我在看,但我是一个巨大的新手在这方面,我有点困惑,我所看到的。。。如果您能提供任何额外的见解,我们将不胜感激。。。我将继续尝试找出答案……因此,如果我理解我看到的内容,我可以导入argparse parser=argparse.ArgumentParser.add_参数('-c',action=“store”,dest=“config”)config=ConfigParser.ConfigParser()config.readfp(open(config))user3739525:试试看。