python中else缩进出问题_python中的try,except,else块出现缩进错误

关于我问的一个问题here,我一直在整理一个错误异常。

我已经从另一个文件中整理出一个文件列表,但是如果引用的文件之一不存在,我就很难处理。

我希望能够识别错误,通过电子邮件发送出去,然后创建文件供以后使用。

但是,我输入的“try,except,else”块出现缩进错误。

看起来应该有用,但我就是跑不动!

啊!救命!!!

日志文本文件包含以下内容:

//server-1/data/instances/devapp/log/audit.log

//server-1/data/instances/devapp/log/bizman.db.log

//server-1/data/instances/devapp/log/foo.txt;服务器上不存在此文件

我的代码如下。我认为最好是完整地发布它,而不是一个片段,以防它是在程序的早期的东西,使它崩溃!import os, datetime, smtplib

today = datetime.datetime.now().strftime('%Y-%m-%d')

time_a = datetime.datetime.now().strftime('%Y%m%d %H-%M-%S')

checkdir = '/cygdrive/c/bob/python_'+ datetime.datetime.now().strftime('%Y-%m-%d')+'_test'

logdir = '/cygdrive/c/bob/logs.txt'

errors = '/cygdrive/c/bob/errors.txt'

#email stuff

sender = 'errors@company.com'

receivers = 'bob@company.com'

message_1 = """From: errors

To: Bob

Subject: Log file not found on server

A log file has not been found for the automated check.

The file has now been created.

"""

#end of email stuff

try:

os.chdir (checkdir) # Try opening the recording log directory

except (OSError, IOError), e: # If it fails then :

os.mkdir (checkdir) # Make the new directory

os.chdir (checkdir) # Change to the new directory

log = open ('test_log.txt', 'a+w') #Create and open log file

else :

log = open ('test_log.txt', 'a+w') #Open log file

log.write ("***starting file check at %s ***\r\n\r\n"%tme_a)

def log_opener (logdir) :

with open(logdir) as log_lines: #open the file with the log paths in

for line in log_lines: # for each log path do :

timestamp = time_a + (' Checking ') + line + ( '\r\n' )

try:

with open(line.strip()) as logs:

except (OSError,IOError), e:

try:

smtpObj = smtplib.SMTP('localhost')

smtpObj.sendmail(sender, receivers, message)

log.write (timestamp)

log.write ("Log file not found. Email sent. New file created.")

except SMTPException:

log.write (timestamp)

log.write ("Log file not found. Unable to send email. New file created.")

else : # The following is just to see if there is any output... More stuff to be added here!

print line + ( '\r\n' )

log.write (timestamp)

print "".join(logs.readlines())

print log_opener(logdir)

我得到的错误如下:$ python log_5.py

File "log_5.py", line 38

except (OSError,IOError), e:

^

IndentationError: expected an indented block

据我所知,它应该有用,但是。。。

另外一点,我学习的时间不长,所以我的很多代码都是从各种教程中修改的,或者是从这里或者网上其他地方借来的。

我可能犯了一个非常基本的错误,所以如果我是,请容忍我!

非常感谢你的帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值