Spaces:
Running
Running
File size: 1,604 Bytes
0b27752 |
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 |
import shutil
from pathlib import Path
from typing import List, Tuple
import mimetypes
def get_file_info(file_path: str) -> Dict:
"""Get file information"""
path = Path(file_path)
if not path.exists():
return {'error': 'File not found'}
stat = path.stat()
mime_type, _ = mimetypes.guess_type(str(path))
return {
'name': path.name,
'size': stat.st_size,
'size_mb': round(stat.st_size / (1024 * 1024), 2),
'extension': path.suffix,
'mime_type': mime_type,
'created': stat.st_ctime,
'modified': stat.st_mtime
}
def ensure_directory(directory: str):
"""Ensure directory exists"""
Path(directory).mkdir(parents=True, exist_ok=True)
def copy_file(src: str, dst: str) -> bool:
"""Copy file safely"""
try:
ensure_directory(str(Path(dst).parent))
shutil.copy2(src, dst)
return True
except Exception as e:
print(f"Copy error: {e}")
return False
def move_file(src: str, dst: str) -> bool:
"""Move file safely"""
try:
ensure_directory(str(Path(dst).parent))
shutil.move(src, dst)
return True
except Exception as e:
print(f"Move error: {e}")
return False
def list_files(directory: str, extension: str = None) -> List[str]:
"""List files in directory"""
path = Path(directory)
if not path.exists():
return []
if extension:
return [str(f) for f in path.glob(f"*{extension}")]
else:
return [str(f) for f in path.glob("*") if f.is_file()] |