簡體   English   中英

比較python中的變量

[英]Comparing Variables in python

我將修改后的日期和文件大小保存到單獨行的文本文件中。 然后我打開文本文件,逐行閱讀並將舊的修改日期和文件大小與新文件進行比較。 即使他們確實匹配,我也不能為我的生活讓python同意他們是一樣的。 我做錯了什么,我該如何糾正呢?

def check(movFile):
    lastModified = "%s" % time.ctime(os.path.getmtime(movFile))
    fileSize = "%s" % os.path.getsize(movFile)
    if os.path.isfile(outFile):
        checkFile = open(outFile, "r")
        line = checkFile.readlines()
        line0 = line[0]
        line1 = line[1]
        if lastModified == line0:
            print "last modified are the same"
        if fileSize == line1:
            print "file size is the same)

這是一個文本文件的示例:

Mon Jul  8 12:32:16 2013
7165528

我可以看到舊的和新的都是相同的打印到shell,所以不知道為什么python說它們不一樣。

readlines()讀取整行 ,包括EOL字符。 你需要你比較或使用之前剝離這個人物in運營商(或startswith()方法:

if lastModified == line0.strip():

應該適合你。

嘗試使用刪除空格

 if lastModified.strip() == line0.strip():
        print "last modified are the same"
    if fileSize.strip() == line1.strip():
        print "file size is the same"

如果這不起作用,其他原因可能是數據類型。 嘗試將all轉換為字符串類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM