python try except,Python try / except不起作用

Trying to get the try/except statement working but having problems. This code will take a txt file and copy the file that is in location row 0 to location of row 1. It works however if i change one of the paths to invalid one it generates an error ftplib.error_perm however the except command is not picking up and everything stops. What am i doing wrong? Python 2.4

import csv

import operator

import sys

import os

import shutil

import logging

import ftplib

import tldftp

def docopy(filename):

ftp = tldftp.dev()

inf = csv.reader(open(filename,'r'))

sortedlist = sorted(inf, key=operator.itemgetter(2), reverse=True)

for row in sortedlist:

src = row[0]

dst = row[1]

tldftp.textXfer(ftp, "RETR " + src, dst)

def hmm(haha):

result = docopy(haha);

try:

it = iter(result)

except ftplib.error_perm:

print "Error Getting File"

if __name__ == "__main__":

c = sys.argv[1]

if (c == ''):

raise Exception, "missing first parameter - row"

hmm(c)

解决方案

The except clause will only catch exceptions that are raised inside of their corresponding try block. Try putting the docopy function call inside of the try block as well:

def hmm(haha):

try:

result = docopy(haha)

it = iter(result)

except ftplib.error_perm:

print "Error Getting File"

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值