import os
import re
patt = re.compile (r"sub([0-9]+)_([0-9]+).(in|out)")
cnt = { }
print ("将 subXXX_XXX.in/out 转化为 dataXXX.in/out 的脚本")
path = input ("转化目录(如果是当前目录,输入 .):")
os.chdir (path)
for i in os.listdir ("."):
if os.path.isdir (i):
print ("跳过子目录 %s" % i)
continue
q = re.findall (patt, i)
if not q or len (q) != 1:
print ("%s 不匹配格式 subXXX_XXX.in 或 subXXX_XXX.out,跳过" % i)
continue
q = q[0]
subid = int (q[0])
pid = int (q[1])
suff = q[2]
try:
cnt[subid] += 1
except KeyError:
cnt[subid] = 1
cnt[0] = 0
for i in range (1, len (cnt)):
try:
cnt[i] += cnt[i - 1]
except KeyError:
raise Exception ("错误:subtask 编号不是连续的!自 %d 处断开" % i)
for i in range (1, len (cnt)):
if cnt[i] % 2 == 1:
raise Exception ("Subtask #%d 中 in/out 不配对(文件数量是奇数)" % i)
cnt[i] /= 2
for i in range (1, len (cnt)):
os.mkdir ("subtask%d" % i)
print ("已创建子目录 subtask%d" % i)
copied = 0
covered = []
for i in os.listdir ("."):
if os.path.isdir (i):
continue
q = re.findall (patt, i)
if not q or len (q) != 1:
continue
q = q[0]
subid = int (q[0])
pid = int (q[1])
suff = q[2]
src = i
newname = "data%d.%s" % (cnt[subid - 1] + pid, suff)
dst = "subtask%d/" % subid + newname
print ("%s -> %s" % (src, dst), end='')
if os.path.exists (dst):
print ("(覆盖!)")
covered.append (dst)
else:
print ("")
os.replace (src, dst)
copied += 1
print ("已复制 %d 个文件,覆盖了 %d 个文件" % (copied, len (covered)))
print ("被覆盖文件:")
if len (covered) == 0:
print ("(无)")
else:
for i in covered:
print ("--> \t", i)
subxx2datax.py,将 subXXX_XXX.in/out 转化为 dataXXX.in/out 的脚本
2025-04-22 21:04:21 By ryp
评论
暂无评论
发表评论
可以用@mike来提到mike这个用户,mike会被高亮显示。如果你真的想打“@”这个字符,请用“@@”。