概述
有问题的部分来自这个循环:
for (path,dirs,files) in os.walk("data/"): for image in files: #do something to the image
现在,该脚本在数据目录的第一级工作,但无法处理数据的子目录.
我尝试使用os.path.join():
for (path,files) in os.walk("data/"): print os.path.join(path,dirs)
但是这引发了以下情况:
Traceback (most recent call last): File "bench.py",line 26,in <module> print os.path.join(path,dirs) File "/usr/lib/python2.7/posixpath.py",line 75,in join if b.startswith('/'): AttributeError: 'list' object has no attribute 'startswith'
简而言之,我想要做的是组装从数据到图像的路径,其中包括数据的子目录.这样做的最佳做法是什么?
for path,files in os.walk('data/'): for f in files: fname = os.path.join(path,f) assert(os.path.exists(fname))
dirs是目录路径中的目录列表.你可以实际修改dirs以防止os.walk进入某些目录(整洁!).
总结
以上是编程之家为你收集整理的python – 正确使用os.path和os.join全部内容,希望文章能够帮你解决python – 正确使用os.path和os.join所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧