如何在目录遍历中检查管道?

2024-05-15 05:35:05 发布

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

我有一个Python程序,它遍历一个目录并生成md5sums。有一个文件被卡住了。运行ls -lA我发现它具有prw-------属性。后来我在谷歌上搜索了一下,发现这是一根管子。你知道吗

如何在遍历中检查管道?我只想跳过这个文件。你知道吗


我的遍历代码是:

for dirpath, _, files in walk(folder):
    for fname in files:
        print join(dirpath, fname)
        if not islink(join(dirpath, fname)):
            # do something with the file, here I pass it to myClass.
            myClass.addFile(dirpath, fname)

Tags: 文件in程序目录for属性myclassfiles
1条回答
网友
1楼 · 发布于 2024-05-15 05:35:05

您实际上不需要显式地测试管道,只需要查找文件。你知道吗

使用os.path.isfile();它将为管道返回False,但为实际文件返回True

$ ls -l
total 0
-rw-rw-r  1 mj mj 0 Sep  7 12:27 actualfile
prw-rw-r  1 mj mj 0 Sep  7 12:25 pipe

>>> os.path.isfile('pipe')
False
>>> os.path.isfile('actualfile')
True

相关问题 更多 >

    热门问题