为什么我的python脚本不起作用?

时间:2016-06-28 01:40:55

标签: python python-3.x indentation

我继续犯同样的错误:

  

文件“backup.py”,第26行   logging.error(“无法创建backup.zip”)
  IndentationError:unindent与任何外部缩进级别都不匹配

这是我的剧本:

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()

2 个答案:

答案 0 :(得分:0)

第17行和第48行有错误。logging.error("Error Msg: " = err[1].args[1])

答案 1 :(得分:0)

尝试修复缩进。

从你的例子中很难看到你的尝试除了语句的确切位置,但我已经根据我认为你想要做的事情做了一个我认为是正确缩进的代码片段。

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()