【发布时间】:2019-11-17 16:44:51
【问题描述】:
如何检测目录中是否存在特定文件? 例子: 我想检测目录(dir)中是否存在(.txt)文件 我试过这个:
import os.path
from os import path
def main():
print ("file exist:"+str(path.exists('guru99.txt')))
print ("File exists:" + str(path.exists('career.guru99.txt')))
print ("directory exists:" + str(path.exists('myDirectory')))
if __name__== "__main__":
main()
但所有这些功能都必须以格式(.txt)插入完整的文件名
谢谢!
【问题讨论】:
-
您已经在检查文件是否存在?是否要检查是否存在任何 .txt 文件?
-
您没有使用绝对路径,因此仅测试给定文件名是否存在于当前工作目录中。