2 回答
TA贡献1843条经验 获得超7个赞
使用re和os模块:
PS:如果出现问题,请始终保留原始文件的副本,以便再次使用。
import os,re
file = os.listdir()
match = []
for i in file:
t = re.findall('_ses\-d(.*?)_',i)
if t :
if t[0] not in match :
match.append(t[0])
else :
os.remove(i)
TA贡献1841条经验 获得超3个赞
我试图让它尽可能简单。我希望这有帮助:
import os
directory = 'directory_name' # put in the directory you want to search through
duplicate_file_lst = []
# loop through directory files
for filename in os.listdir(directory):
if filename.startswith("sub-OAS30027_ses-d1300"):
duplicate_file_lst.append(filename)
# Only keeps the first file in the list
for file in duplicate_file_lst:
if file != duplicate_file_lst[0]:
os.remove(file)
添加回答
举报