【发布时间】:2018-05-05 06:03:36
【问题描述】:
我正在尝试将文本添加到来自 -a 开关的配置文件。 其余代码确实有效,但不确定谁调用选择配置文件并写入一个新文件以备份到其中。
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')
parser.add_argument('-l', '--location', default="/home/admin/Documents/backup/",help='Choose location to store files')
parser.add_argument('-a', '--addfile', help='Choose a file to add to list')
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:
logger.error("Config file not found")
print ("Config File not found")
def add_to_file():
try:
f = open('configfile','r')
f.read()
addto = f.write('addfile')
f.close()
except FileNotFoundError:
pass**
args = vars(parser.parse_args())
read = read_config(args['configfile'])
add = add_to_file(args['addfile'])
当我运行它时,我收到如下错误:
add = add_to_file(args['addfile'])
TypeError: add_to_file() takes 0 positional arguments but 1 was given
有什么想法我会出错吗?
感谢您的帮助
【问题讨论】:
标签: python python-3.x append argparse