如何将一些特定的 .jpg 文件复制到另一个目录?
how to copy some specific .jpg files to another directory?
我想将一些特定的jpg文件复制到另一个目录,但我不明白为什么它不起作用?我有很多图像,目前只想对某些类别进行排序,这些类别的开头名称分别为 15_0_xxx.jpg 15_1_xxx.jpg
import cv2
import sys
import os
import shutil
from os import listdir
from os.path import isfile, join
mypath = "c:/Users/Harum/Desktop/make dir/"
file_names = [ f for f in listdir(mypath) if isfile(join(mypath, f))]
print(str(len(file_names))+ ' images loaded')
cont_M =0
cont_F =0
m_age = "c:/Users/Harum/Desktop/make dir/M_15/"
f_age = "c:/Users/Harum/Desktop/make dir/F_15/"
input_m = []
input_mS =[]
input_fS =[]
input_f = []
def getZeros(number):
if(number > 10 and number <100):
return "0"
if(number < 10):
return "00"
else:
return ""
for i, file in enumerate(file_names):
if file_names[i][0] == "15_0":
cont_M +=1
image = cv2.imread(mypath+file)
input_m.append(image)
input_mS.append(0)
zeros = getZeros(cont_M)
cv2.imwrite(m_age +"m_age"+str(zeros)+ str(cont_M)+ ".jpg",image)
if file_names[i][0] == "15_1":
cont_F +=1
image = cv2.imread(mypath+file)
input_f.append(image)
input_fs.append(1)
cv2.imwrite(f_age+"F_age"+str(zeros)+ str(cont_M)+ ".jpg",image)
`
编辑:忘记了复制部分。您可以为此使用 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}")
#(...)
我想将一些特定的jpg文件复制到另一个目录,但我不明白为什么它不起作用?我有很多图像,目前只想对某些类别进行排序,这些类别的开头名称分别为 15_0_xxx.jpg 15_1_xxx.jpg
import cv2
import sys
import os
import shutil
from os import listdir
from os.path import isfile, join
mypath = "c:/Users/Harum/Desktop/make dir/"
file_names = [ f for f in listdir(mypath) if isfile(join(mypath, f))]
print(str(len(file_names))+ ' images loaded')
cont_M =0
cont_F =0
m_age = "c:/Users/Harum/Desktop/make dir/M_15/"
f_age = "c:/Users/Harum/Desktop/make dir/F_15/"
input_m = []
input_mS =[]
input_fS =[]
input_f = []
def getZeros(number):
if(number > 10 and number <100):
return "0"
if(number < 10):
return "00"
else:
return ""
for i, file in enumerate(file_names):
if file_names[i][0] == "15_0":
cont_M +=1
image = cv2.imread(mypath+file)
input_m.append(image)
input_mS.append(0)
zeros = getZeros(cont_M)
cv2.imwrite(m_age +"m_age"+str(zeros)+ str(cont_M)+ ".jpg",image)
if file_names[i][0] == "15_1":
cont_F +=1
image = cv2.imread(mypath+file)
input_f.append(image)
input_fs.append(1)
cv2.imwrite(f_age+"F_age"+str(zeros)+ str(cont_M)+ ".jpg",image)
`
编辑:忘记了复制部分。您可以为此使用 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}")
#(...)