Python(2.7):简单版本检查器

时间:2014-03-02 02:26:55

标签: python python-2.7 if-statement urllib

前言:我是Python的初学者。我已经尝试了学习指南,但我很难学习,所以我正在尝试制作一个超级简单的更新检查器,以便我慢慢开始。我抓住了一些我在这里找到的代码并修改了一下,唉,它不起作用。它读取本地和外部.txt文件并打印它们的输出(只是为了检查它是否正确读取它们)。然后它以某种方式在if / elif / elif / else语句中失败,所以一些帮助就会很棒!

目前它正在告诉我“NameError:全局名称'我'未定义”但是我此时遇到了几个不同的错误,我真的只是在寻找解决方案,从那里我可以向后工作。谢谢!

import Tkinter
import urllib
import time

print "test"

#previously self within the brackets
def updateCheck():
    update = False

    updateWindow = Tkinter.Toplevel()
    updateWindow.title(string="Update Checker")
    updateWindow.resizable(False, False)

    #gets local version (file currently says "1.0")
    localSource = open('version.txt', 'r')
    localContents = localSource.read()
    print "local version = " + localContents

    #gets server version (file currently says "1.1")
    serverSource = urllib.urlopen("http://raw.github.com/SamHH/ccr-version/master/version.txt")
    serverContents = serverSource.read()
    print "server version = " + serverContents

    #checks for updates by comparing the above two -- doesn't work
    if serverContents[i] > localContents[i]:
        dataLabel = Tkinter.Label(updateWindow,text="\n\nThere is an update available.\n\n")
        dataLabel.pack()
        #need a way of opening a .url file in the same folder here, if possible
    elif serverContents[i] < localContents[i]:
        dataLabel = Tkinter.Label(updateWindow,text="\n\nYour installation appears to be broken.\n\n")
        dataLabel.pack()
        #need a way of opening a .url file in the same folder here, if possible, again
    elif serverContents[i] == localContents[i]:
        versionLabel = Tkinter.Label(updateWindow,text="\n\nYou are already running the most up to date version.\n\n")
        versionLabel.pack()
        #need a way of opening a .exe file in the same folder this time, if possible
    else:
        versionLabel = Tkinter.Label(updateWindow,text="\n\nYour install is corrupted. Doh!\n\n")
        versionLabel.pack()

updateCheck()

2 个答案:

答案 0 :(得分:0)

如果本地和删除'文件'只包含一个浮点数,请从每个文件中读取一行并将其转换为float(),以便比较:

try:
    localSource = open('version.txt', 'r')
    localContents = float(localSource.readline())
except (IOError, ValueError):
    versionLabel = Tkinter.Label(updateWindow,text="\n\nYour install is corrupted. Doh!\n\n")
    versionLabel.pack()
    return

serverSource = urllib.urlopen("http://raw.github.com/SamHH/ccr-version/master/version.txt")
serverContents = float(serverSource.readline())

然后使用localContentsserverContents名称进行比较:

if serverContents > localContents:
    # etc.

答案 1 :(得分:0)

if serverContents[i] > localContents[i]:

请注意,您从未将i初始化为默认值。它正在查找你的代码,看你是否已经定义并将其设置在函数之外(你没有)。

加入循环

for i in range(len(serverContents)):

您还应该检查两个列表的大小是否相同,否则当您尝试索引结束时会出现错误。

请注意,这假设serverContents和localContents都是列表,每个列表的元素都是要比较的值。如果内容是文本字符串。然后你将循环遍历字符串中的每个字符。如果这是你想要的,你不需要这样做

f = '1.1a'
g = '1.1a'

f == g # shows True
f is g # shows False

这意味着'1.1a'和'01 .1a'将显示不同的

但是,这将允许版本号不是完全数字的情况,如果您使用float(serverContents),这是一个要求。