upgraded copy method
This commit is contained in:
+21
-6
@@ -40,12 +40,27 @@ def umount_sd(path='/sd'):
|
||||
sd_ok = False
|
||||
return sd_ok
|
||||
|
||||
# TODO: switch to buffered copying to avoid RAM overflow
|
||||
def copy(p1, p2):
|
||||
with open(p1, 'rb') as f1:
|
||||
content = f1.read()
|
||||
with open(p2, 'wb') as f2:
|
||||
f2.write(content)
|
||||
def copy(source_path, dest_path, buffer_size=8192):
|
||||
"""
|
||||
Copies a file from source_path to dest_path
|
||||
"""
|
||||
try:
|
||||
with open(source_path, "rb") as src:
|
||||
with open(dest_path, "wb") as dst:
|
||||
while True:
|
||||
# Read a chunk from the source
|
||||
buffer = src.read(buffer_size)
|
||||
if not buffer:
|
||||
break
|
||||
# Write the chunk to the destination
|
||||
dst.write(buffer)
|
||||
return True
|
||||
except OSError as e:
|
||||
print(f"Error copying file: {e}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"An unexpected error occurred: {e}")
|
||||
return False
|
||||
|
||||
cp = copy # shortcut export
|
||||
|
||||
|
||||
Reference in New Issue
Block a user