簡體   English   中英

python argparse.ArgumentParser讀取配置文件

[英]python argparse.ArgumentParser read config file

我試圖添加開關-c並指定配置文件。 我現在使用config.dat使它工作,但是當我使用-c並指定一個新的.dat時,它將使用默認的config.dat...。

知道我哪里出錯了嗎?

#!/usr/bin/python3
import argparse
import shutil

parser = argparse.ArgumentParser(description='Copy multiple Files from a specified data file')
parser.add_argument('-c', '--configfile', default="config.dat",help='file to read the config from')


def read_config(data):
    try:
        dest = '/home/admin/Documents/backup/'
        #Read in date from config.dat
        data = open('config.dat')
        #Interate through list of files '\n'
        filelist = data.read().split('\n')
        #Copy through interated list and strip white spaces and empty lines
        for file in filelist:
            if file:
                shutil.copy(file.strip(), dest)
    except FileNotFoundError:
        pass

args =parser.parse_args()
read = read_config(args.configfile)
args =parser.parse_args()

仔細看看第14行的操作。即使您正在檢索--configfile參數並將其分配給args您仍在使用字符串文字data = open('config.dat')而不是傳遞data (是作為參數傳遞給函數read_config的configfile的參數read_config

def read_config(data):
    try:
        dest = '/home/admin/Documents/backup/'
        #Read in date from config.dat
        data = open(data)
        ...

我還將更改傳遞給read_config的參數data的命名-這有點模棱兩可。 您知道此函數需要文件名作為參數,所以為什么不簡單地將其稱為filename

def read_config(filename):
    import pdb; pdb.set_trace()
    try:
        dest = '/home/admin/Documents/backup/'
        #Read in date from config.dat
        data = open(filename)
        #Interate through list of files '\n'
        filelist = data.read().split('\n')
        #Copy through interated list and strip white spaces and empty lines
        for file in filelist:
            if file:
                shutil.copy(file.strip(), dest)
    except FileNotFoundError:
        pass

適當使用函數參數; 更改名稱以闡明變量的性質。

def read_config(filename='config.dat'):
    try:
        dest = '/home/admin/Documents/backup/'
        afile = open(filename)
        #Interate through list of files '\n'
        filelist = afile.read().split('\n')
        #Copy through interated list and strip white spaces and empty lines
        for file in filelist:
            if file:
                shutil.copy(file.strip(), dest)
    except FileNotFoundError:
        pass

該代碼通過將args轉換為字典,然后通過鍵獲取值來工作。 另外,您在第13行上的代碼沒有打開傳入的值。 這將打開傳入的文件。 看看這是否適合您:

# !/usr/bin/python3
import argparse
import shutil

parser = argparse.ArgumentParser(description='Copy multiple Files from a specified data file')
parser.add_argument('-c', '--configfile', default="config.dat", help='file to read the config from')


def read_config(data):
    try:
        dest = '/home/admin/Documents/backup/'
        # Read in date from config.dat
        data = open(data)
        # Interate through list of files '\n'
        filelist = data.read().split('\n')
        # Copy through interated list and strip white spaces and empty lines
        for file in filelist:
            if file:
                shutil.copy(file.strip(), dest)
    except FileNotFoundError:
        pass


args = vars(parser.parse_args())
read = read_config(args['configfile'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM