gpt4 book ai didi

python - 如何检查python目录中是否只存在txt文件?

转载 作者:太空宇宙 更新时间:2023-11-04 06:58:57 25 4
gpt4 key购买 nike

我必须在 while 循环中检查。我发现以下代码显示了没有 while 循环的文件。

for file in glob.glob("*.txt"):
print(file)

但是如果我使用下面的代码,它在 while 循环中不起作用

a = os.listdir(my_path)
#print(a)
for file in glob.glob("*.txt"):
print(file)

我正在尝试使用 OS 模块在 python 中编写一个 watcher 类型

最佳答案

while True:
f = os.listdir(path)
if len(f) > 0:
for i in os.listdir(path):
if i.endswith('.txt'):
continue
else:
dosomething()
time.sleep(5) #so loop checks files every 5 sec

这样你只使用 os 模块,不需要 glob。

关于python - 如何检查python目录中是否只存在txt文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51166970/

25 4 0
文章推荐: html - Shift
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com