PythonElse发布了一个FTP程序

PythonElse发布了一个FTP程序,python,if-statement,syntax,ftp,try-catch,Python,If Statement,Syntax,Ftp,Try Catch,我对这个程序的else语句有异议。。。我已经检查了我的间距,它似乎是正确的。我在else语句中不断发现语法错误。程序创建文件,然后尝试将其上载到ftp服务器,但如果它没有对用户说任何话,只是继续,它将在程序循环时重试。如果您能提供任何帮助,我们将不胜感激 #IMPORTS import ConfigParser import os import random import ftplib from ftplib import FTP #LOOP PART 1 from time import sl

我对这个程序的else语句有异议。。。我已经检查了我的间距,它似乎是正确的。我在else语句中不断发现语法错误。程序创建文件,然后尝试将其上载到ftp服务器,但如果它没有对用户说任何话,只是继续,它将在程序循环时重试。如果您能提供任何帮助,我们将不胜感激

#IMPORTS
import ConfigParser
import os
import random
import ftplib
from ftplib import FTP
#LOOP PART 1
from time import sleep
while True:
    #READ THE CONFIG FILE SETUP.INI
    config = ConfigParser.ConfigParser()
    config.readfp(open(r'setup.ini'))
    path = config.get('config', 'path')
    name = config.get('config', 'name')
    #CREATE THE 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 Full Path
    fullpath = path + name
    #Random Sleep to Accomidate FTP Server
    sleeptimer = random.randrange(1,30+1)
    sleep((sleeptimer))
    #Upload File to 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 is Wrong'
    #LOOP PART 2
    sleep(180.00)

else
作为异常块的一部分是有效的,但只有在未引发异常且之前必须定义了除之外的
的异常时,才会运行该异常

(编辑)大多数人跳过else子句,在退出try/except子句(dedenting)后编写代码

快速教程包括:

try:
    # some statements that are executed until an exception is raised
    ...
except SomeExceptionType, e:
    # if some type of exception is raised
    ...
except SomeOtherExceptionType, e:
    # if another type of exception is raised
    ...
except Exception, e:
    # if *any* exception is raised - but this is usually evil because it hides
    # programming errors as well as the errors you want to handle. You can get
    # a feel for what went wrong with:
    traceback.print_exc()
    ...
else:
    # if no exception is raised
    ...
finally:
    # run regardless of whether exception was raised
    ...

你的意思是除了
?!这是我的理解,我需要使用其他。。。说到编程,我有点新手。我可以推荐1。将
else:
替换为
,例外情况除外:
2<代码>例外情况除外:应该与
try:
处于相同的缩进级别,谢谢你的帮助。。。我为这个问题道歉,但这对我来说是一个新领域。。。再次感谢