检查文件是否在子目录中?

1991年

我想检查给定文件是否在特定目录内,并且包括子目录。

我使用此代码:

def Grep(givenfile,givendir):
    # iterate top down till in directory path
    for (root,dirs,files) in os.walk(givendir):
        if givenfile in files:
            # if file present return true
            return True
    return False

## Test.py my file that contain my code 
##C://Users//LENOVO//Desktop//python2021 this my path that contain my files 
Grep("Test.py",r"C://Users//LENOVO//Desktop//python2021")

当我运行没有显示输出的代码吗?

有没有更好的方法来检查给定文件是否在特定目录中,并且包括子目录

谢谢

胞期

没有输出,因为您什么都不打印。将此作为最后一行:

print(Grep("Test.py",r"C://Users//LENOVO//Desktop//python2021"))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章