Python中文
首页
教程
问答
标签
搜索
登录
注册
Python fnmatch,检查exist fi
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有一个任务,我要检查目录中是否存在特殊文件*.part。 如果文件存在,请再次检查,否则,打印“文件已删除”。在</p> <p>我使用操作系统列表目录(),接下来对于lisdir中的每个文件,我使用fnmatch(file,'*.part'),接下来再次获取lisdir并递归地涉及相同的函数。在</p> <p>当我删除这个文件时,fnmatch返回“true”。我不明白……”为什么“?在</p> <p>这里是我的代码:</p> <pre><code>import os, fnmatch def func(filenames): for f in filenames: while fnmatch.fnmatch(f, '*.part'): filenames = os.listdir("/home/gromov/Downloads/test/") func(filenames) if __name__ == "__main__": func(os.listdir("/home/gromov/Downloads/test/")) print("file was deleted") </code></pre> <p>谢谢!在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>您使用的是递归,可能是从中获取的文件列表操作系统列表目录()每次都返回相同的文件列表,因此在更新之前返回True。在</p> <p>试着用非递归的方式写这篇文章,并确保从中检索到的列表操作系统列表目录()不是每次迭代都一样。在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在乒乓球比赛中预测球的轨迹,对于AI球拍预测?
1 回答
如何在乒乓球游戏中阻止球
2 回答
如何在乘法和模中不乘空间?
10 回答
如何在乘法和除以2个不同的数字之间进行交换?
10 回答
如何在也是数据一部分的单个字符上拆分大字符串
1 回答
如何在乾草堆中找到針,有更好的解決方案嗎?
4 回答
如何在事件wxWidgets中传递自定义数据
2 回答
如何在事件中使用lambda i=i?
5 回答
如何在事件中心只接收最近的数据
8 回答
如何在事件发生之前保持云函数运行?
5 回答
如何在事件发生后使页面重定向到同一页面
5 回答
如何在事件回调之间保持python生成器的状态
2 回答
如何在事件处理程序(pythonsocket、sphinx)中保留docstring
6 回答
如何在事件处理程序中更改wxRichTextCtrl的光标位置?
9 回答
如何在事件处理程序中访问外部对象?
3 回答
如何在事件循环中将协程打包为正常函数?
7 回答
如何在事件循环之外运行协同程序?
9 回答
如何在事件循环结束时为并发未来的所有线程调用类方法?
1 回答
如何在事件文件中只保留一份摘要?
4 回答
如何在事件模板中添加事件
1 回答