Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.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 - Fatal编程技术网

如果文件不存在,python将失败

如果文件不存在,python将失败,python,Python,所以我有一个循环,检查文件夹是否包含*.zip文件 for file in glob.glob( os.path.join(rootdir, '*.zip')): print "zip", file #Check if file does not exist if not os.path.exists(file): print "No files" #return else:

所以我有一个循环,检查文件夹是否包含*.zip文件

for file in glob.glob( os.path.join(rootdir, '*.zip')):
        print "zip", file
        #Check if file does not exist
        if not os.path.exists(file):
            print "No files"
            #return
        else:
            some code
现在,如果文件存在,其他方法可以工作,但如果没有zip文件,则不会进行打印

有什么建议吗??
谢谢

如果该文件夹中没有zip文件,for循环将永远不会执行

就像

for i in []:
    print "Hello"
不打印任何内容,因为没有可迭代的元素

如果需要该错误消息,可以执行以下操作:

filelist = glob.glob(os.path.join(rootdir, '*.zip'))
if filelist:
    for f in filelist:
        # some code
else:
    print "No files"

如果该文件夹中没有zip文件,则for循环永远不会执行

就像

for i in []:
    print "Hello"
不打印任何内容,因为没有可迭代的元素

如果需要该错误消息,可以执行以下操作:

filelist = glob.glob(os.path.join(rootdir, '*.zip'))
if filelist:
    for f in filelist:
        # some code
else:
    print "No files"

如果没有zip文件,则在空列表上循环,请尝试以下操作:

files = glob.glob( os.path.join(rootdir, '*.zip'))
if len(files) != 0:
    for file in files:
        print "zip", file
        #Check if file does not exist
        if not os.path.exists(file):
            print file, "does not exist"
            #return
        else:
            some code
else:
    print "No files"

如果没有zip文件,则在空列表上循环,请尝试以下操作:

files = glob.glob( os.path.join(rootdir, '*.zip'))
if len(files) != 0:
    for file in files:
        print "zip", file
        #Check if file does not exist
        if not os.path.exists(file):
            print file, "does not exist"
            #return
        else:
            some code
else:
    print "No files"

glob()
文件返回找到的任何匹配项。如果未找到与您提供的模式匹配的项,则返回空列表:
[]
。这与shell glob模式的功能不同!在大多数shell中,当没有以
foo*
开头的文件时,
foo*
只会将单词
foo*
赋予命令;但这不是
glob()
所做的。

glob()文件返回它找到的任何匹配项。如果未找到与您提供的模式匹配的项,则返回空列表:
[]
。这与shell glob模式的功能不同!在大多数shell中,当没有以
foo*
开头的文件时,
foo*
只会将单词
foo*
赋予命令;但是这不是glob()所做的。

如果没有文件存在,那么您的循环将根本不会运行。test os.path.exists(文件)将始终为true(除非在运行循环时其他进程正在删除文件),否则glob不会将is作为文件列出

files = glob.glob( os.path.join(rootdir, '*.zip'))
for file in files:
        print "zip", file
if len(files) == 0:
        print "no files"

如果没有文件存在,那么您的循环将根本不会运行。test os.path.exists(文件)将始终为true(除非在运行循环时其他进程正在删除文件),否则glob不会将is作为文件列出

files = glob.glob( os.path.join(rootdir, '*.zip'))
for file in files:
        print "zip", file
if len(files) == 0:
        print "no files"

glob.glob
将只返回确实存在的文件。通过测试文件是否存在,您试图执行什么操作?“文件打印未发生”?有两个
print
语句。你能说得更具体些吗?
glob.glob
只返回确实存在的文件。通过测试文件是否存在,你想做什么?“文件打印不发生”?有两个
print
语句。你能说得更具体一点吗?谢谢。这是一个很好的解决方案。谢谢。这是一个很好的解决方案