1 回答

TA贡献1810条经验 获得超4个赞
编辑:忘记了复制部分。你可以使用 shutil 来做到这一点
使用 glob 和 os 会做得更好:
from shutil import copyfile
import glob
import os
mypath = "c:/Users/Harum/Desktop/make dir/"
destination_path = "c:/Users/Harum/Desktop/copy/"
# using fstrings to add wildcard character to consider all files. You could add a
## file extension after, as in f"{mypath}15_*.jpg"
file_names = glob.glob(f"{mypath}15_*")
# skip the middle to the ifs
# (...)
# removed the enumerate as it doesn't seems like you're using the positional list index
for file in file_names:
# getting only the filename (with extension)
file_name = os.path.basename(file)
# using the str().startswith() to check True or False
if file_name.startswith("15_0"):
cont_M +=1
copyfile(file, f"{destination_path}{file_name}")
# (...)
elif file_name.startswith("15_1"):
cont_F +=1
copyfile(file, f"{destination_path}{file_name}")
#(...)
添加回答
举报