Python功能正确/不正确?

问题描述:

我刚开始使用Python作为学习体验。我知道编程的基本逻辑。我在每次执行程序时都会运行python中的函数,即使它不应该运行。我在函数的开头使用了一个if语句,但我不知道为什么这个if语句不起作用。我有另一个类似的功能,并正常工作。我错过了一些简单的东西吗Python功能正确/不正确?

下面是不正常的功能...

def check_artist_art(): 
if os.path.exists("/tmp/artistinfo") and open("/tmp/artistinfo").read() != title: 
#if artist == "": 
    if os.path.exists(home + "/.artist"): 
     os.remove(home + "/.artist") 
    if os.path.exists("/tmp/artistinfo"): 
     os.remove("/tmp/artistinfo") 
     print artist 
    return False 
else: 
    os.path.exists("/tmp/artistinfo") and open("/tmp/artistinfo").read() == artist 
    return False 
return True 

这是正常工作的功能相似..

def check_album(): 
if os.path.exists("/tmp/albuminfo") and open("/tmp/albuminfo").read() != album: 
    if os.path.exists(home + "/.album"): 
     os.remove(home + "/.album") 
    if os.path.exists("/tmp/albuminfo"): 
     os.remove("/tmp/albuminfo") 
    return False 
elif os.path.exists("/tmp/trackinfo") and open("/tmp/trackinfo").read() == artist + album: 
    return False 
return True 

任何帮助是极大的赞赏。

+0

你能否解释一下什么是“不工作”的意思?你有错误吗? – Blender 2012-03-19 02:01:39

+0

使用'上下文管理器':D – Doboy 2012-03-19 02:23:21

你对此有何看法?

+0

如果该文件存在并且它不等于之前在python脚本中定义的变量。 – 2012-03-19 10:10:15

首先 - 你是什么意思'不工作'? 第二 - 指定艺术家,专辑和标题变量? ;第三,它看起来像下面的代码:

需要被改为:

elif os.path.exists("/tmp/artistinfo") and open("/tmp/artistinfo").read() == artist: 
+0

我很抱歉让我们解释更多。我使用这个python程序来收集moc音乐信息。变量艺术家,专辑和标题是先前用mocp命令定义的。你引用的代码行是elif,我只是改变它,看看它是否会有所作为,并忘记在我发布之前将它改回来。 – 2012-03-19 10:15:44

+0

即使我用当前艺术家手动创建/ tmp /艺术家,我说的功能也不起作用,删除/删除.artist和/ tmp/artistinfo文件。文件.artist和/ tmp/artist会被创建,然后在几秒钟后移除。如果我发布完整的程序会更有帮助吗? – 2012-03-19 10:28:07

+0

@Anthony - 所有我可以建议你在每个if语句之前和之后添加调试打印语句。这将有助于您找到文件未被删除的原因。 – 2012-03-19 13:45:32