Python 将随机选择的图像放入新文件夹
我有一个包含大量子目录的目录 在每个子目录中都有不同的JPEG、PNG 我想: 从这些子目录中选择X数量的随机图像 创建一个新文件夹并将这些选定的随机图像复制到其中 感谢这里已经收到的帮助,我可以使用os.walk和random.choice打印随机选择的图像 然而,我的问题是实际选择随机图像,而不是它们的名称 我尝试创建一个使用os.walk的变量imagePathPython 将随机选择的图像放入新文件夹,python,image,random,shutil,os.walk,Python,Image,Random,Shutil,Os.walk,我有一个包含大量子目录的目录 在每个子目录中都有不同的JPEG、PNG 我想: 从这些子目录中选择X数量的随机图像 创建一个新文件夹并将这些选定的随机图像复制到其中 感谢这里已经收到的帮助,我可以使用os.walk和random.choice打印随机选择的图像 然而,我的问题是实际选择随机图像,而不是它们的名称 我尝试创建一个使用os.walk的变量imagePath #creates a variable imagePath that lets me access all img files
#creates a variable imagePath that lets me access all img files in different folders
imagePath = os.walk("/Path/to/Directory")
以及一个新变量,用于从imagePath中随机选择单个图像
然后创建一个新目录,并使用shutil.copy将正常选择的图像移动到此新目录中
#creates a new directory
os.mkdir('testDirectory')
#moves the randomly selected image into new directory
shutil.copy(randomImages, testDirectory)
但是,我得到以下错误:
Traceback (most recent call last):
File "crawl.py", line 28, in <module>
randomImages = random.choice(os.listdir(imagePath))
TypeError: coercing to Unicode: need string or buffer, generator found
但这会返回随机选择的子目录(不包括图像)以及.ds存储文件。您应该能够为shutil.copy提供源文件和目标文件路径。在我看来,你已经有了一个文件列表,所以你可以直接复制它们
import os
import random
import shutil
files_list = []
for root, dirs, files in os.walk("/Path/to/Directory"):
for file in files:
#all
if file.endswith(".jpg") or file.endswith(".png") or file.endswith(".jpeg"):
files_list.append(os.path.join(root, file))
#print images
#lets me count and print the amount of jpeg,jpg,pmg
file_count = len(files_list)
print file_count
print files_list
selected_files = random.sample(files_list, 2)) #assign to a list
dest_path = "/path/to/new/folder/"
os.mkdir(dest_path)
for src_path in selected_files:
shutil.copy(src_path, os.path.join(dest_path, os.path.basename(src_path)))
这是代码,你想移动文件,而不是复制另一个我猜
import os
import random
import shutil
files_list = []
for root, dirs, files in os.walk("<SOURCE_DIR>"):
for file in files:
#all
if file.endswith(".jpg") or file.endswith(".png") or file.endswith(".jpeg"):
files_list.append(os.path.join(root, file))
#print images
#lets me count and print the amount of jpeg,jpg,pmg
file_count = len(files_list)
print file_count
# print files_list
filesToCopy = random.sample(files_list, 2) #prints two random files from list
destPath = "<DESTINATION_DIR>"
# if destination dir does not exists, create it
if os.path.isdir(destPath) == False:
os.makedirs(destPath)
# iteraate over all random files and move them
for file in filesToCopy:
shutil.move(file, destPath)
我认为问题在于imagePath,os.listdirimagePath,os.listdir将指向目录的字符串路径作为参数,imagePath不是,尝试集成这行代码,迭代结果并获取dirs。对于os.walk.中的root、dirs和文件,top-down=False:os.walk-doc,[谢谢@n0thing我已经尝试过了,并用新版本更新了我的OP。
for root, dirs, files in os.walk("/Path/to/Directory", topdown=False):
imagePath = ("/Path/to/Directory") #creates a variable that lets me access all img files in different folders
randomImages = random.choice(os.listdir(imagePath))
print randomImages
import os
import random
import shutil
files_list = []
for root, dirs, files in os.walk("/Path/to/Directory"):
for file in files:
#all
if file.endswith(".jpg") or file.endswith(".png") or file.endswith(".jpeg"):
files_list.append(os.path.join(root, file))
#print images
#lets me count and print the amount of jpeg,jpg,pmg
file_count = len(files_list)
print file_count
print files_list
selected_files = random.sample(files_list, 2)) #assign to a list
dest_path = "/path/to/new/folder/"
os.mkdir(dest_path)
for src_path in selected_files:
shutil.copy(src_path, os.path.join(dest_path, os.path.basename(src_path)))
import os
import random
import shutil
files_list = []
for root, dirs, files in os.walk("<SOURCE_DIR>"):
for file in files:
#all
if file.endswith(".jpg") or file.endswith(".png") or file.endswith(".jpeg"):
files_list.append(os.path.join(root, file))
#print images
#lets me count and print the amount of jpeg,jpg,pmg
file_count = len(files_list)
print file_count
# print files_list
filesToCopy = random.sample(files_list, 2) #prints two random files from list
destPath = "<DESTINATION_DIR>"
# if destination dir does not exists, create it
if os.path.isdir(destPath) == False:
os.makedirs(destPath)
# iteraate over all random files and move them
for file in filesToCopy:
shutil.move(file, destPath)