python - 检查文件是否在子目录中?
问题描述
我想检查给定文件是否在特定目录中,并且它包括子目录。
我使用了这段代码:
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"))
推荐阅读
- java - Java纸牌游戏“Skat”
- git - git: git 获取
以“xxx/xxx 似乎不是 git 存储库”结尾 - amazon-web-services - ssm:resourceTag 跨账户问题
- python - 无法使用“python”命令从命令提示符运行 python;它只需要'py -3'
- java - 如何从 Java 中的第三方 Rest 端点下载文件
- javascript - Vue.js 异步组件不更新模板
- javascript - CSS用javascript中的类更改id
- python - 所以,我在我的 django 项目中进行更新,我收到反向匹配错误
- python - 在另一个 python 文件中导入 python 文件
- r - x 轴图 R 的色点