路径的os.listdir包含空白

时间:2014-02-14 11:34:30

标签: python python-2.7 os.path listdir

这个非常奇怪的问题我的代码在我的机器上工作正常(Linux mint)但它在我的服务器中显示错误

这里是代码-note我已经成功abspath即使它在我的机器上没有它也能正常工作!

def GET_Contents(filepath):
    return os.listdir(os.path.abspath(os.path.join('files', filepath.strip())))

这是给定的例外 return os.listdir(os.path.abspath(os.path.join('files', filepath.strip()))) OSError: [Errno 2] No such file or directory: '/home/hamoud/webapps/FileManager/files/EE201/MID 1'

只有包含空格的文件夹(如MID 1

才会出现问题

1 个答案:

答案 0 :(得分:1)

空格很好,也允许在Windows路径中使用。

您需要验证您的工作目录是否正确(因为您使用相对路径,os.path.abspath()将基于当前工作目录的绝对路径)。

如果基本路径正确,请检查该位置是否 一个MID 1目录。也许首先用os.listdir(os.path.abspath('files'))检查目录,看看那里到底是什么。