-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfileCopier.py
More file actions
40 lines (29 loc) · 1.46 KB
/
Copy pathfileCopier.py
File metadata and controls
40 lines (29 loc) · 1.46 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
#This Python script helps you copy files of a specific type (e.g., PDF) from a source directory to a target directory. It checks each file in the source
#directory and its subdirectories. If the file matches the specified type, and it hasn't been copied before, it copies it to the target directory. The
#script then prints out the total number of files copied.
import os
import shutil
def copy_files(source_dir, target_dir, file_extension, visited_dirs):
count = 0
for root, dirs, files in os.walk(source_dir):
if root not in visited_dirs:
for file in files:
if file.endswith(f".{file_extension}"):
source_file = os.path.join(root, file)
target_file = os.path.join(target_dir, file)
if not os.path.exists(target_file):
try:
shutil.copy2(source_file, target_dir)
count += 1
except Exception as e:
print(f"Error copying file {source_file}: {e}")
visited_dirs.add(root)
return count
type = str(input("Enter type of file -->"))
source_dir = "D:/"
target_dir = f"D:/{type}_files"
if not os.path.exists(target_dir):
os.makedirs(target_dir)
visited_dirs = set()
count = copy_files(source_dir, target_dir, type, visited_dirs)
print(f"Total {count} files of type {type} copied to {target_dir}")