os.listdir(路径)是否包含特殊条目'。'和'..'?

时间:2015-12-13 22:25:33

标签: python python-2.7 pydev

我尝试访问的路径是../Pictures/NewYork

self.path = "../Pictures/NewYork"
self.files = os.listdir(self.path )

我收到一条错误消息,说

The system cannot find the path specified: '../Pictures/NewYork/*.*'

有没有更好的方法升级?

1 个答案:

答案 0 :(得分:0)

只是一个疯狂的客人......你想从py文件所在的地方上去?

os.listdir("../Pictures/NewYork")将与当前工作目录相关,可能是任何内容。

如果我猜对了,你想要这个:

this_dir = os.path.dirname(__file__)
new_york_dir = os.path.join(this_dir, '..', 'Pictures', 'NewYork')
files = os.path.listdir(new_york_dir)