我正在构建一个基本的文件服务器,但我的程序找不到文件。
def sendfile(sock, myfile):
print 'Serving file:', myfile
print 'File exists?:', os.path.exists(myfile)
path = os.path.normpath(os.path.join(os.getcwd(), myfile))
print 'Serving file:', path
print 'File exists?:', os.path.exists(path)
这些代码总是返回False,即使'myfile'和'path'是正确的[文件与服务器程序在同一目录下]。IDLE正常工作,但不适用于传递到函数中。
>>> print os.path.exists("/user/server/foo.txt")
True
我错过了什么?
[编辑:] 输出:
Serving file: foo.txt
File exists?: False
Serving file: /user/server/foo.txt
File exists?: False
myfile
究竟是什么?/user/server/foo.txt
? - Pierre GMimport pdb; pdb.set_trace()
,并检查本地范围。path
末尾是否有换行符?在将path
传递给os.path.exists
之前,尝试去除路径中的空格。 - Thane Brimhall