Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正在计算目录中的图像数:不工作_Python_Python 2.7 - Fatal编程技术网

Python 正在计算目录中的图像数:不工作

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目录路径部分)。

我正在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目录路径部分)。您需要将目录名与文件名连接起来

[f for f in os.listdir('Images') if os.path.isfile(os.path.join('Images', f))]