我正在努力处理文件。你知道吗
我有两个这样的文件:
Document 1 987.docx
Document 1 Abc.docx
我有一个变量:
x = "Document 1.docx"
如何创建if
语句来表示。。
如果Document 1.docx
和1
后面的任意单词在文件夹中出现两次,那么Print("True")
这就是我要说的:
import os
import glob
directory = "C:/Users/hawk/Desktop/Test" # directory
choices = glob.glob(os.path.join(directory, "*")) # returns all files in the folder
print(choices) #shows all files from the directory
x = "Document 1.docx"
这听起来像是正则表达式的工作:
这将捕获任何以“Document 1”开头,后跟空格的文件名,但不包括例如“Document 1”名称.docx". 例如:
应该有用(只要没有标点符号)。你知道吗
您可以使用
glob
语法筛选文件,然后检查是否找到任何内容:相关问题 更多 >
编程相关推荐