-2
config.dat (addtofile 섹션)에 행을 추가하려고합니다. 이 작업은 한 번 작동합니다. 여기에서 두 번째 줄을 추가하려고하면 실패합니다.python - 새 줄에 텍스트 파일에 추가
raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
shutil.SameFileError: 'backup/file1' and '/home/admin/Documents/backup/file1' are the same file
코드는 위의 내용에 대해 불평하고 있습니다. 제안 사항.
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 addtofile(add_config):
try:
with open('config.dat', 'a') as file:
file.write(add_config + "\n")
except FileNotFoundError:
logger.error("error message")
print ("error message here")
args = vars(parser.parse_args())
read = read_config(args['configfile'])
add = addtofile(args['add'])
StackOverflow에 오신 것을 환영합니다. 도움말 설명서의 게시 지침을 읽고 따르십시오. [최소한의 완전하고 검증 가능한 예제] (http://stackoverflow.com/help/mcve)가 여기에 적용됩니다. MCVE 코드를 게시하고 문제를 정확하게 설명하기 전까지는 효과적으로 도움을 드릴 수 없습니다. 게시 된 코드를 텍스트 파일에 붙여넣고 설명한 문제를 재현 할 수 있어야합니다. – Prune