1
-a 스위치에서 설정 파일에 텍스트를 추가하려고합니다. 나머지 코드는 작동하지만 select config 파일을 호출하고 백업 할 새 파일을 작성할 사람이 누구인지 확실하지 않습니다. 내가이 실행 파일에 텍스트를 추가하십시오. 지정된 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', '--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
어떤 아이디어 나는이 문제 하겠어 어디? 어떤 도움
도움 주셔서 감사합니다. 나는 변경 다음 데프 addtofile (시험) : 시도 : 개방 ('config.dat', 'A')와 파일로 : FileNotFoundError 제외 file.write가 (테스트 + "\ n을") : logger.error ("오류 메시지") 인수의 = 용 VARS (parser.parse_args()) 판독 = read_config (인수 [ '에서 configFile']) 추가 = addtofile (인수는 [ '추가']) 또 shutil .SameFileError : 'backup/test.dat'및 '/home/admin/Documents/backup/test.dat'는 동일한 파일입니다. –