-5

I keep on getting the same error:

File "backup.py", line 26
logging.error("Unable to create backup.zip")
IndentationError: unindent does not match any outer indentation level

This is my script:

import sys
import os
import logging

logging.basicConfig(filename='file_ex.log', level = logging.DEBUG)

logging.info("checking to see if the backup.zip exists")

if os.path.exists("backup.zip"):
    logging.info("It exists!")
try:
    zip_file = zipfile.ZipFile('backup.zip','a')
except:
    err = sys.exc_info()
    logging.error("Unable to open backup.zip in append mode")
    logging.error("Error Num: " + str(err[1].args[0]))
    logging.error("Error Msg: " = err[1].args[1])
    sys.exit()

else:
    logging.info("Creating backup.zip")
try:
   zip_file = zipfile.ZipFile('backup.zip', 'w')
except:
    err = sys.exc_info()
    logging.error("Unable to create backup.zip")
    logging.error("Error Num: " + str(err[1].args[0]))
    logging.error("Error Msg: " + err[1].args[1])
    sys.exit()

else:
    logging.info("Creating backup.zip")
try:
   zip_file = zipfile.ZipFile('backup.zip', 'w')
except:
    err = sys.exc_info()
    logging.error("Unable to create backup.zip")
    logging.error("Error Num: " + str(err[1].args[0]))
    logging.error("Error Msg: " + err[1].args[1])
    logging.info("adding test.txt to backup.zip")

try:
    zip_file.write('test.txt', 'test.txt', zipfile.ZIP_DEFLATED)
except:
    err = sys.exc_info()
    logging.error("Unable to open backup.zip in append mode")
    logging.error("Error Num: " + str(err[1].args[0]))
    logging.error("Error Msg: " = err[1].args[1])
    zip_file.close()
zondo
  • 19,901
  • 8
  • 44
  • 83

2 Answers2

0

You have errors on line 17 and 48. logging.error("Error Msg: " = err[1].args[1])

p.magalhaes
  • 7,595
  • 10
  • 53
  • 108
0

Try fix your indentation.

It is hard from your example to see exactly where your try except statements belong but I have done a code snippet with what I believe would be correct indentation based on what I believe you are trying to do.

import sys
import os
import logging

logging.basicConfig(filename='file_ex.log', level = logging.DEBUG)

logging.info("checking to see if the backup.zip exists")

if os.path.exists("backup.zip"):
  logging.info("It exists!")
  try:
    zip_file = zipfile.ZipFile('backup.zip','a')
  except:
    err = sys.exc_info()
    logging.error("Unable to open backup.zip in append mode")
    logging.error("Error Num: " + str(err[1].args[0]))
    logging.error("Error Msg: " = err[1].args[1])
    sys.exit()
else:
  logging.info("Creating backup.zip")
  try:
    zip_file = zipfile.ZipFile('backup.zip', 'w')
  except:
    err = sys.exc_info()
    logging.error("Unable to create backup.zip")
    logging.error("Error Num: " + str(err[1].args[0]))
    logging.error("Error Msg: " + err[1].args[1])
    sys.exit()
  else:
    logging.info("Creating backup.zip")
    try:
     zip_file = zipfile.ZipFile('backup.zip', 'w')
    except:
      err = sys.exc_info()
      logging.error("Unable to create backup.zip")
      logging.error("Error Num: " + str(err[1].args[0]))
      logging.error("Error Msg: " + err[1].args[1])
      logging.info("adding test.txt to backup.zip")
try:
  zip_file.write('test.txt', 'test.txt', zipfile.ZIP_DEFLATED)
except:
  err = sys.exc_info()
  logging.error("Unable to open backup.zip in append mode")
  logging.error("Error Num: " + str(err[1].args[0]))
  logging.error("Error Msg: " = err[1].args[1])
  zip_file.close()
zondo
  • 19,901
  • 8
  • 44
  • 83
Aidan
  • 757
  • 3
  • 13