【发布时间】:2018-06-30 06:59:23
【问题描述】:
在这里,我正在尝试创建一个代码,该代码将根据掩码删除文件夹中的文件。应删除所有包含 17 的文件,文件夹中文件的一般格式为 ??_????17*.* 哪里? - 任何符号 1..n,A..z; * - 任意长度的符号; _ 和 17 - 在任何文件中(其他文件也包含 18),其扩展名无关紧要。文件夹中文件的某些示例:AB_DEFG17Something.Anything - Copy (2).txt; AB_DEFG18Something.Some - 复制 (3).txt... p.s.为之前的不充分和不准确的解释道歉。如果文件名称相似,您对 globe.globe 的看法是正确的。 很高兴收到有关此任务的观点,我希望它对其他人有用。
import os
import re
dir_name = "/Python/Test_folder" # open the folder and read files
testfolder = os.listdir(dir_name)
def matching(r, s): # condition if there's nothing to match
match = re.search(r, s)
if match:
return "Files don't exist!"
matching(r'^\w\w\[_]\w\w\w\w\[1]\[7]\w+\[.]\w+', testfolder) # matching the mask of files
for item in testfolder.index(matching):
if item.name(matching, s):
os.remove(os.path.join(dir_name, item))
# format of filenames not converted : ??_????17*.*
【问题讨论】:
-
请用更清晰的方式重写您的问题
-
您为什么不使用here 提供的答案之一 - 与您提出完全相同的问题???