.
├── input
│ └── _sampled
└── src
└── random_sampling.py
random_smpling.py
from pprint import pprint
import os
from pathlib import Path
import random
import shutil
class FileControler(object):
def make_file(self, output_dir, file_name, content='') -> None:
"""File creation"""
#Create if there is no output directory
os.makedirs(output_dir, exist_ok=True)
#Combine output directory and file name
file_path = os.path.join(output_dir, file_name)
#File creation
with open(file_path, mode='w') as f:
#Create an empty file by default
f.write(content)
def get_files_path(self, input_dir, pattern):
"""Get file path"""
#Create a path object by specifying a directory
path_obj = Path(input_dir)
#Match files in glob format
files_path = path_obj.glob(pattern)
#Posix conversion to treat as a character string
files_path_posix = [file_path.as_posix() for file_path in files_path]
return files_path_posix
def random_sampling(self, files_path, sampl_num, output_dir, fix_seed=True) -> None:
"""Random sampling"""
#If you sample the same file every time, fix the seed value
if fix_seed is True:
random.seed(0)
#Specify the file group path and the number of samples
files_path_sampled = random.sample(files_path, sampl_num)
#Create if there is no output directory
os.makedirs(output_dir, exist_ok=True)
#copy
for file_path in files_path_sampled:
shutil.copy(file_path, output_dir)
file_controler = FileControler()
Create 100 files in ʻinput / Copy the sampled file into ʻinput / _sampled
all_files_dir = '../input'
sampled_dir = '../input/_sampled'
50 files each of .py
and .txt
for i in range(1, 51):
file_controler.make_file(all_files_dir, f'file{i}.py')
file_controler.make_file(all_files_dir, f'file{i}.txt')
from pprint import pprint
pattern = '*.py'
files_path = file_controler.get_files_path(all_files_dir, pattern)
pprint(files_path)
# ['../input/file8.py',
# '../input/file28.py',
# '../input/file38.py',
# .
# .
# .
# '../input/file25.py',
# '../input/file35.py',
# '../input/file50.py']
#
print(len(files_path))
# 50
sample_num = 10
file_controler.random_sampling(files_path, sample_num, sampled_dir)
Terminal
ls input/_sampled
file10.py file23.py file3.py file35.py file36.py file37.py file38.py file4.py file41.py file43.py
Recommended Posts