forked from fractalboi21/learning-journey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove_files.py
75 lines (61 loc) · 2.22 KB
/
move_files.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import shutil
from pathlib import Path
from datetime import datetime
def move_files_to_folder(source_dir, new_folder_name):
"""
Move all files from source directory to a new folder,
except requirements.txt and README.md
"""
# Convert to Path object
source_path = Path(source_dir)
# Create timestamp for folder name
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
new_folder_name = f"{new_folder_name}_{timestamp}"
# Create new folder
new_folder_path = source_path / new_folder_name
new_folder_path.mkdir(exist_ok=True)
print(f"Created folder: {new_folder_name}")
# Files to exclude
excluded_files = {
'requirements.txt',
'README.md',
'readme.md',
'move_files.py' # Exclude this script itself
}
# Move files
moved_count = 0
for item in source_path.iterdir():
# Skip if it's a directory or an excluded file
if item.is_dir() or item.name.lower() in excluded_files:
continue
try:
# Handle name conflicts
target_path = new_folder_path / item.name
if target_path.exists():
base = target_path.stem
suffix = target_path.suffix
counter = 1
while target_path.exists():
new_name = f"{base}_{counter}{suffix}"
target_path = new_folder_path / new_name
counter += 1
# Move the file
shutil.move(str(item), str(target_path))
print(f"Moved: {item.name} -> {target_path.name}")
moved_count += 1
except Exception as e:
print(f"Error moving {item.name}: {str(e)}")
print(f"\nOperation complete!")
print(f"Files moved: {moved_count}")
print(f"Destination folder: {new_folder_path}")
def main():
# Get current directory
current_dir = os.getcwd()
print("File Moving Utility")
print("==================")
print(f"Working directory: {current_dir}")
# Move files to a new folder named 'project_files'
move_files_to_folder(current_dir, "project_files")
if __name__ == "__main__":
main()