Python 正在计算目录中的图像数:不工作
我正在Windows7上运行Python 2.7.6。我在Python 正在计算目录中的图像数:不工作,python,python-2.7,Python,Python 2.7,我正在Windows7上运行Python 2.7.6。我在C:/Data/Images有一个目录,其中包含许多JPEG图像。因此,我在命令提示符下导航到C:/Data,然后通过输入Python运行pythonshell。然后,我输入以下内容: print len([f for f in os.listdir('Images') if os.path.isfile(f)]) 这将返回0,尽管我知道C:/Data/images中有几个图像 这是为什么?返回文件名列表(wihtout目录路径部分)。
C:/Data/Images
有一个目录,其中包含许多JPEG图像。因此,我在命令提示符下导航到C:/Data
,然后通过输入Python
运行pythonshell。然后,我输入以下内容:
print len([f for f in os.listdir('Images') if os.path.isfile(f)])
这将返回0
,尽管我知道C:/Data/images
中有几个图像
这是为什么?返回文件名列表(wihtout目录路径部分)。您需要将目录名与文件名连接起来
[f for f in os.listdir('Images') if os.path.isfile(os.path.join('Images', f))]