如何比较字典中的值

时间:2014-05-15 05:28:42

标签: python-2.7 dictionary

目前我正在开展一个小项目,但我仍然遇到这个代码的问题。

import xmlrpclib
import glob
import os
from SimpleXMLRPCServer import SimpleXMLRPCServer

# keep track of all files in directory
fileList = {} 

# Search for all files in directory
for file in glob.glob("*.txt"):
    fileList[file] = "closed"

def openFile(filename):
    if filename in fileList.keys(): # Check if file exists
        value = fileList.get(filename)
        if value == 'closed'          # If closed then open
            fileList[filename] = open(filename, 'r+b')
            return "File open"
        else                          # File must have been open
            return "File is already open"
    else                            # File doesn't exist, create one
        # if file not found
        fileList[filename] = open(filename, 'w') # Create file
        return "File created"

def closeFile(filename):
    if filename in fileList.keys(): # Check if file exists
        fileList[filename].close()    # close file
        fileList[filename] = 'closed' # Change value
        return "File closed"
    else
    return "file does not exist"

def readFile(filename):              
    if filename in fileList.keys():    # Check if file exists
        return fileList[filename].read() # Return contents
    else
        return "File does not exist"

def writeFile(filename, content):
    if filename in fileList.keys():     # Check if file exists
        fileList[filename].write(content) # Write content to file
        return "Wrtie successful"
    else
        return "File does not exist"

server = SimpleXMLRPCServer(("localhost", 8000)) # Giving server a port
print "Listening on port 8000..."
server.register_function(openFile,'openFile')
server.register_function(closeFile,'closeFile')
server.register_function(readFile, 'readFile')
server.register_function(writeFile, 'writeFile')
server.serve_forever()

由于某种原因,我一直在:

File "server.py", line 16
    if value == 'closed'          # If closed then open
                                                      ^
SyntaxError: invalid syntax

正如您所看到的,语法错误,但我不知道为什么和任何帮助将不胜感激。

我试图删除评论但是我得到了相同的错误消息,箭头指向if语句中的最后一个字符。

我是python的新手,但有一年的Java经验,所以如果这也发生在Java中,我可能会更好地理解Java术语。

1 个答案:

答案 0 :(得分:0)

你忘记了冒号:

if value == 'closed':
                    ^-----colon