python - Python 2.7 check if there is a file with a specific file name
问题描述
I want to check if there is a file in a folder that its name contains a specific string, like:
folder:
-- hi.txt
-- bye.txt
-- bedanagain.txt
-- dan1.txt
-- dan2.txt
-- gray.txt
return true if in the folder there is a file with "dan" in its name.
Plus, If possible, I need to use the most well known imports (the customer is nervous about using unknown open source packages) and the smallest number of imports.
I tried using os.path.exists but I found that it works only if you have the full filename, and it wasn't able to search for a file name that contains some string.
I tried:
import os
print os.path.exists('./dan') // false
print os.path.exists('./dan*') // false
print os.path.exists('./*dan') // false
That is also why this is not a duplicate - I checked the mentioned feed, it deals with "if a file exists" and not "if a file with some string in its name exists"
Thanks!
解决方案
您可以使用该OS
模块。
前任:
import os
for file in os.listdir(Path):
if "dan" in file:
print(file)
推荐阅读
- android - NavigationView 中的分隔线(在 BottomSheetDialogFragment 中)未显示?
- shell - 比较 csv 文件中每一行的第一个单词并打印匹配的行,失败时还打印与第一个单词不匹配的行
- php - .env 文件在 laravel 中不存在
- android - 列出 Android 和 iOS 中可用的测试用例
- php - 使用 PHP 来自 SQL Server 数据库的 Google 图表
- arrays - 我想根据需要动态数组。在 python 中是否有可能,是的,那么如何?
- swift - 在swift中继承tableview单元deque错误
- reactjs - 我们可以根据用户滚动的程度使用 ReactJS 为元素设置动画吗
- java - InetAddress 不是线程安全的?
- java - 安卓套接字程序