I’m building a basic file server and my program cannot find files.
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)
These always return False even though the ‘myfile’ and ‘path’ are correct [the file is in the same directory as the server program].
IDLE works fine, but without passing to functions.
>>> print os.path.exists("/user/server/foo.txt")
True
What have I missed?
[EDIT:] Output:
Serving file: foo.txt
File exists?: False
Serving file: /user/server/foo.txt
File exists?: False
I’m almost 100% sure you’re not sanitizing your input before you check if the path exists. Here’s something I ran in my interpreter:
Try stripping whitespace on
pathbefore you check if it exists.