在不引用现有路径的路径上使用os.listdir时会发生此错误。
例如:>>> os.listdir(‘Some 便宜香港vps directory does not exist’)
Traceback (most recent call last):
File “”, line 1, in
WindowsError: [Error 3] : ‘Some directory does not exist/*.*’
如果要使用os.listdir,则需要确保要使用的路径存在,或者首先使用os.path.exists检查存在性。if os.path.exists(‘/client_side/’):
do something
else:
do something
假设您当前的工作目录是c:\foobar,os.listdir(‘/client_side/’)相当于os.listdir(‘c:/client_side’),而os.listdir(‘client_side/’)相当于os.listdir(‘c:/foobar/client_side’)。如果客户端目录不在根目录中,则在使用os.listdir时将发生此类错误。
对于您的输出问题,让我们回忆一下^{}Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries ‘.’ and ‘..’ even if they are present in the directory.
以及^{}。Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.
listdir既不返回绝对路径也不返回相对路径,而是返回文件名的列表,而isfile需要路径。因此,所有这些名称都会产生False。
为了获得路径,我们可以直接使用^{},concat两个字符串。print ([name for name in os.listdir(path)
if os.path.isfile(os.path.join(path, name))])
或者print ([name for name in os.listdir(‘client_side/’)
if os.path.isfile(‘client_side/’ + name)])