Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
120 lines (104 loc) · 3.3 KB
/
Copy pathapp.py
File metadata and controls
120 lines (104 loc) · 3.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os
import shutil
import streamlit as st
import time
# Function to map file extensions to folder names
def get_folder_name(extension):
extension_map = {
"image": [".png", ".jpg", ".jpeg", ".gif"],
"document": [".pdf", ".docx", ".txt", ".xls", ".xlsx", ".pptx"],
"archive": [".zip", ".rar", ".tar", ".gz"],
"audio": [".mp3", ".wav", ".aac"],
"video": [".mp4", ".mkv", ".avi", ".mov"],
"code": [".py", ".js", ".html", ".css", ".java", ".c", ".cpp"],
}
for folder, extensions in extension_map.items():
if extension in extensions:
return folder + "-files"
return "other-files"
# Function to sort files in the given folder
def sort_files_in_folder(path):
if not os.path.exists(path):
return "The specified folder does not exist. Please check the path."
file_names = os.listdir(path)
for file in file_names:
file_path = os.path.join(path, file)
# Skip directories
if os.path.isdir(file_path):
continue
# Get file extension and determine folder
_, extension = os.path.splitext(file.lower())
folder_name = get_folder_name(extension)
# Create folder if it doesn't exist
folder_path = os.path.join(path, folder_name)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# Move the file to the appropriate folder
try:
shutil.move(file_path, os.path.join(folder_path, file))
except Exception as e:
return f"Error moving file {file}: {e}"
return None
# Streamlit app setup
st.set_page_config(
page_title="Automatic File Sorter", page_icon="📂", layout="centered"
)
st.markdown(
"""
<style>
body {
background-color: #fdfffc;
}
.stTextInput > div > input {
font-size: 16px;
color: #235789;
}
.loader {
border: 5px solid #fdfffc;
border-radius: 50%;
border-top: 5px solid #235789;
width: 30px;
height: 30px;
animation: spin 2s linear infinite;
margin: 20px auto;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
""",
unsafe_allow_html=True,
)
st.title("📂 Automatic File Sorter")
st.markdown(
"### **Easily organize your files by type!**\n\n"
"Enter the folder path below and click **Sort Files** to automatically organize your files. ",
unsafe_allow_html=True,
)
# Input: Path to the folder
folder_path = st.text_input(
"Folder Path", placeholder="Enter the path to the folder you want to sort"
)
# Button to trigger sorting
if st.button("Sort Files"):
if folder_path:
# Show the loader
with st.spinner("Sorting files... Please wait!"):
time.sleep(3) # Simulate processing time
result = sort_files_in_folder(folder_path)
# Display result
if result:
st.error(result)
else:
st.success("File sorting complete!")
st.markdown(
"""
<script>
alert("File sorting complete!");
</script>
""",
unsafe_allow_html=True,
)
else:
st.error("Please enter a valid folder path.")