-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.py
118 lines (95 loc) · 3.28 KB
/
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
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
import os
import shutil
sdPaths = ["/mnt/mmc/Roms", "/mnt/sdcard/Roms"]
def copy_file(source_path, destination_path):
"""
Copies a file from the source path to the destination path.
Parameters:
source_path (str): Full path of the source file.
destination_path (str): Full path of the destination file.
Returns:
str: Success or error message.
"""
try:
# Check if the source file exists
if not os.path.exists(source_path):
return f"Error: The file {source_path} does not exist."
# Create the destination directory if it does not exist
os.makedirs(os.path.dirname(destination_path), exist_ok=True)
# Copy the file
shutil.copy2(source_path, destination_path)
return f"File successfully copied from {source_path} to {destination_path}."
except Exception as e:
return f"Error copying the file: {e}"
def list_filenames(path):
"""
Lists all files in the directory.
Parameters:
path (str): Directory path.
Returns:
list: List of filenames in the directory or an error message.
"""
try:
# List all files in the directory
filenames = [f for f in os.listdir(path) if not os.path.isdir(os.path.join(path, f))]
return filenames
except FileNotFoundError:
print(f"Path {path} does not exist.")
return [f"Path {path} does not exist."]
except Exception as e:
print(f"An error occurred: {e}")
return [f"An error occurred: {e}"]
def list_directories(path):
"""
Lists all directories in the directory.
Parameters:
path (str): Directory path.
Returns:
list: List of directories in the directory or an error message.
"""
try:
# List all directories in the directory
directories = [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))]
return directories
except FileNotFoundError:
print(f"Path {path} does not exist.")
return [f"Path {path} does not exist."]
except Exception as e:
print(f"An error occurred: {e}")
return [f"An error occurred: {e}"]
def get_consoles(sdcard=0):
"""
Gets the list of console directories.
Parameters:
sdcard (int): SD card index.
Returns:
list: List of console directories.
"""
return list_directories(sdPaths[sdcard])
def get_roms(console, sdcard=0):
"""
Gets the list of ROM files for a given console.
Parameters:
console (str): Console directory name.
sdcard (int): SD card index.
Returns:
list: List of ROM files for the console.
"""
return list_filenames(os.path.join(sdPaths[sdcard], console))
def get_rom_imgs(console, sdcard=0):
""">
Gets the list of image files for a given console.
Parameters:
console (str): Console directory name.
sdcard (int): SD card index.
Returns:
list: List of image files for the console.
"""
return list_filenames(os.path.join(sdPaths[sdcard], console, 'Imgs'))
def filter_empty_directories(directories,sdcard=0):
non_empty_dirs = []
for directory in directories:
full_path = os.path.join(sdPaths[sdcard], directory)
if list_filenames(full_path):
non_empty_dirs.append(directory)
return non_empty_dirs