Python 无法列出我的目录
我的 Bottle 应用程序中有这段代码,用于列出一个充满文件的目录:
[Post(name[:-3]) for name in os.listdir("posts")]
在我的本地计算机上,没问题。但在我的服务器上,我一直收到此错误:
OSError: [Errno 2] No such file or directory: 'posts'
os.listdir('.')
和 $ ls
报告该目录在那里。 os.getcwd()
报告我位于父目录中并且应该能够访问它。 glob.glob("posts/*.md")
不返回任何内容,因此我认为这不是 listdir
的问题。通过 ssh 运行 python 并尝试相同的代码是成功的(即:我得到了 Post 对象的列表)。
有什么我错过的吗?
编辑
感谢您的所有建议。问题的根源是 WSGI 配置错误。此后,我使用正确的配置参数重新安装了应用程序,一切都很好。
I have this code in my Bottle app to list a directory full of files:
[Post(name[:-3]) for name in os.listdir("posts")]
On my local computer, it's fine. But on my server I am consistently getting this error:
OSError: [Errno 2] No such file or directory: 'posts'
os.listdir('.')
and $ ls
reports that the directory is there. os.getcwd()
reports that I'm in the parent directory and should be able to access it. glob.glob("posts/*.md")
returns nothing, so I don't think it's a problem with listdir
. Running python
over ssh and trying the same code is successful (ie: I get a list of Post
objects).
Is there something I've missed?
Edit
Thanks for all the advice. The source of the problem was WSGI misconfiguration. I've since re-mounted the application using the proper config arguments, and all is well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先想到的是权限 - 确保您的权限设置为应用程序至少具有读取访问权限。
listdir
仍然会说某些东西存在,即使它无法读取它。接下来的想法是,这看起来像一个相对路径——你确定相对路径是正确的吗?我知道您已经通过
listdir('.')
找到了该目录(所以这应该不重要),但是如果您实际上硬编码该目录的完整路径会发生什么? (很多时候“没关系”确实如此)The first thing that comes to mind is permissions -- make sure that your permissions are set so that the app has at least read access.
listdir
will still say that something exists, even if it can't read it.Next thought is that looks like a relative path -- are you sure that the relative path is correct? I know that you've found the directory through
listdir('.')
(so it shouldn't matter), but what happens if you actually hard code the full path to the directory? (there are plenty of times where "it shouldn't matter" does)