-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_move.py
54 lines (44 loc) · 2.09 KB
/
file_move.py
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
47
48
49
50
51
52
53
54
import os
import shutil
import multiprocessing
def move_files_to_year_wise_folders(root_dir):
# Create a dictionary to store the year-wise folders
year_folders = {}
# Iterate over all files in the root directory
for filename in os.listdir(root_dir):
# Get the 7th and 8th characters from the end of the filename
year = filename[-8:-6]
# Create the year-wise folder if it doesn't exist
year_folder = os.path.join(root_dir, year)
if year not in year_folders:
year_folders[year] = year_folder
if not os.path.exists(year_folder):
os.makedirs(year_folder)
# Move the file to the year-wise folder
src_file = os.path.join(root_dir, filename)
dst_file = os.path.join(year_folder, filename)
shutil.move(src_file, dst_file)
def parallel_move_files_to_year_wise_folders(root_dir, num_processes):
# Create a pool of worker processes
with multiprocessing.Pool(processes=num_processes) as pool:
# Split the files into chunks for parallel processing
files = os.listdir(root_dir)
chunks = [files[i::num_processes] for i in range(num_processes)]
# Process each chunk in parallel
pool.starmap(move_files_to_year_wise_folder_chunk, [(root_dir, chunk) for chunk in chunks])
def move_files_to_year_wise_folder_chunk(root_dir, file_chunk):
for filename in file_chunk:
# Get the 7th and 8th characters from the end of the filename
year = filename[-8:-6]
# Create the year-wise folder if it doesn't exist
year_folder = os.path.join(root_dir, year)
if not os.path.exists(year_folder):
os.makedirs(year_folder)
# Move the file to the year-wise folder
src_file = os.path.join(root_dir, filename)
dst_file = os.path.join(year_folder, filename)
shutil.move(src_file, dst_file)
if __name__ == '__main__':
root_dir = '/path/to/root/directory'
num_processes = multiprocessing.cpu_count() # Use all available CPU cores
parallel_move_files_to_year_wise_folders(root_dir, num_processes)