当我指定-a开关由于某种原因添加到配置文件时,它也会调用read_config文件。例如,我使用./script.py -a newfile.txt并正确添加文件,但随后返回"未找到配置文件"。
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', '--add', help='add new line to config data')
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")
不太确定为什么下面这部分工作但调用了read_config并找到了except错误。我哪里错了?
def addtofile(add):
f = open('config.dat', "a")
f.write(add + '\n')
f.close()
args = vars(parser.parse_args())
read = read_config(args['configfile'])
add = addtofile(args['add'])
答案 0 :(得分:0)
scala> val d = b.map{case(k,v) => (k) -> (
None.asInstanceOf[Option[Any]],Some(v).asInstanceOf[Option[Any]])}
d: org.apache.spark.rdd.RDD[(String, (Option[Any], Option[Any]))] =
MapPartitionsRDD[5] at map at <console>:26
scala> val c = a.map{case(k,v) => (k) ->
(Some(v).asInstanceOf[Option[Any]],None.asInstanceOf[Option[Any]])}
c: org.apache.spark.rdd.RDD[(String, (Option[Any], Option[Any]))] =
MapPartitionsRDD[6] at map at <console>:26
scala> c.union(d)
res1: org.apache.spark.rdd.RDD[(String, (Option[Any], Option[Any]))] =
UnionRDD[7] at union at <console>:33
答案 1 :(得分:0)
我认为您的解决方案将是:
parser = argparse.ArgumentParser()
parser.add_argument('-c', action='store_true')
a = parser.parse_args()
if a.c:
{
print("get custom config")
}
else:
{
print("using default config file")
}