|
|
|
|
|
import os |
|
import shutil |
|
import sys |
|
|
|
|
|
if len(sys.argv) != 2: |
|
print("Usage: python script.py <source_directory>") |
|
sys.exit(1) |
|
|
|
source_dir = sys.argv[1] |
|
base_dir_name = os.path.basename(source_dir.rstrip('/')) |
|
target_dir1 = os.path.join(source_dir, f'{base_dir_name}1') |
|
target_dir2 = os.path.join(source_dir, f'{base_dir_name}2') |
|
|
|
|
|
os.makedirs(target_dir1, exist_ok=True) |
|
os.makedirs(target_dir2, exist_ok=True) |
|
|
|
|
|
files = os.listdir(source_dir) |
|
|
|
|
|
moved_files = {} |
|
|
|
|
|
for file in files: |
|
|
|
if os.path.isdir(os.path.join(source_dir, file)): |
|
continue |
|
|
|
|
|
base_filename = os.path.splitext(file)[0] |
|
|
|
|
|
if base_filename not in moved_files: |
|
target_dir = target_dir1 if len(moved_files) % 2 == 0 else target_dir2 |
|
moved_files[base_filename] = target_dir |
|
else: |
|
target_dir = moved_files[base_filename] |
|
|
|
|
|
shutil.move(os.path.join(source_dir, file), os.path.join(target_dir, file)) |
|
|
|
print("Files have been separated into two directories.") |
|
|
|
|