python argparse.ArgumentParser read config file

Viewed 4197

Im trying to add the switch -c and specify the config file. I have it working at the moment using the config.dat but when i use -c and specify a new .dat it uses the default config.dat....

Any idea where im going wrong?

#!/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()
3 Answers

Take a close look at what you are doing on line 14. Even though you are retrieving and assigning the --configfile argument to args you are still using a string literal data = open('config.dat') instead of passing data (which is the value of your argument for the configfile passed as an argument to the function read_config):

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

I would also change the naming of the argument data you are passing to read_config-- it's a bit ambiguous. You know that this function expects a file name as an argument so why not simply call it 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

Make proper use of the function argument; names changed to clarify the nature of the variables.

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

This code works by converting the args to a dictionary, then getting the value via key. Also, the code you had on line 13 didn't open the passed in value. This one opens the passed in file. See if this works for you:

# !/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'])
Related