Tuesday, 15 February 2011

if statement - Python else issues making an FTP program -



if statement - Python else issues making an FTP program -

i having issue else statement of program... have checked spacing , seems correct. maintain getting syntax error on else statement. programme creates , file attempts upload ftp server if fails not user , go on seek 1 time again when programme loops. help provide appreciated.

#imports import configparser import os import random import ftplib ftplib import ftp #loop part 1 time import sleep while true: #read config file setup.ini config = configparser.configparser() config.readfp(open(r'setup.ini')) path = config.get('config', 'path') name = config.get('config', 'name') #create keyfile filepath = os.path.join((path), (name)) if not os.path.exists((path)): os.makedirs((path)) file = open(filepath,'w') file.write('text here') file.close() #create total path fullpath = path + name #random sleep accomidate ftp server sleeptimer = random.randrange(1,30+1) sleep((sleeptimer)) #upload file ftp server try: host = '0.0.0.0' port = 3700 ftp = ftp() ftp.connect(host, port) ftp.login('user', 'pass') file = open(fullpath, "rb") ftp.cwd('/') ftp.storbinary('stor ' + name, file) ftp.quit() file.close() else: print 'something wrong' #loop part 2 sleep(180.00)

else valid part of exception block, run if exception not raised , there must except defined before it.

(edit) people skip else clause , write code after exiting (dedenting) try/except clauses.

the quick tutorial is:

try: # statements executed until exception raised ... except someexceptiontype, e: # if type of exception raised ... except someotherexceptiontype, e: # if type of exception raised ... except exception, e: # if *any* exception raised - evil because hides # programming errors errors want handle. can # sense went wrong with: traceback.print_exc() ... else: # if no exception raised ... finally: # run regardless of whether exception raised ...

python if-statement syntax ftp try-catch

No comments:

Post a Comment