Python无法区分文件或目录

2024-09-28 23:26:28 发布

您现在位置:Python中文网/ 问答频道 /正文

情况:给定以下Python3代码:

top = "/"
def traverse(top):
    filecount = 0
    dircount = 0
    for root, dirs, files in os.walk(top):
        path = os.path.abspath(root)
        print(path)
        if os.path.isdir(path):
            dircount += 1
        elif os.path.isfile(path):
            filecount += 1
            #put the filepath into a function that hashes, then prints the hash
            md5hash(path)

问题1:有些目录没有被遍历,在那里目录将被打印,但是子目录将不会被打印

问题2:一些目录被当作文件,因此被传递到哈希函数中,而哈希函数反过来会抛出一个错误“它不是一个文件”,实际上告诉我Python函数混淆了文件和目录

附加信息:如果有帮助的话,我现在运行的是elementaryOS(ubuntu14.04)


Tags: 文件thepath函数代码目录ostop