-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyPercentageOfFiles.py
More file actions
47 lines (37 loc) · 1.3 KB
/
CopyPercentageOfFiles.py
File metadata and controls
47 lines (37 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
import shutil
import random
import sys
def copy_random_files(input_dir, output_dir, percentage):
if not os.path.exists(input_dir):
print(f"Input directory does not exist: {input_dir}")
sys.exit(1)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
all_files = [
os.path.join(input_dir, f)
for f in os.listdir(input_dir)
if os.path.isfile(os.path.join(input_dir, f))
]
if not all_files:
print("No files found in input directory.")
sys.exit(1)
sample_size = max(1, int(len(all_files) * (percentage / 100.0)))
selected_files = random.sample(all_files, sample_size)
for file_path in selected_files:
shutil.copy(file_path, output_dir)
print(f"Copied: {file_path} -> {output_dir}")
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: python script.py <input_folder> <output_folder> <percentage>")
sys.exit(1)
input_folder = sys.argv[1]
output_folder = sys.argv[2]
try:
percentage = float(sys.argv[3])
if not (0 < percentage <= 100):
raise ValueError()
except ValueError:
print("Percentage must be a number between 0 and 100.")
sys.exit(1)
copy_random_files(input_folder, output_folder, percentage)