Maheen001 commited on
Commit
0b27752
·
verified ·
1 Parent(s): 401cf22

Create utils/file_utils.py

Browse files
Files changed (1) hide show
  1. utils/file_utils.py +65 -0
utils/file_utils.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import shutil
2
+ from pathlib import Path
3
+ from typing import List, Tuple
4
+ import mimetypes
5
+
6
+
7
+ def get_file_info(file_path: str) -> Dict:
8
+ """Get file information"""
9
+ path = Path(file_path)
10
+
11
+ if not path.exists():
12
+ return {'error': 'File not found'}
13
+
14
+ stat = path.stat()
15
+ mime_type, _ = mimetypes.guess_type(str(path))
16
+
17
+ return {
18
+ 'name': path.name,
19
+ 'size': stat.st_size,
20
+ 'size_mb': round(stat.st_size / (1024 * 1024), 2),
21
+ 'extension': path.suffix,
22
+ 'mime_type': mime_type,
23
+ 'created': stat.st_ctime,
24
+ 'modified': stat.st_mtime
25
+ }
26
+
27
+
28
+ def ensure_directory(directory: str):
29
+ """Ensure directory exists"""
30
+ Path(directory).mkdir(parents=True, exist_ok=True)
31
+
32
+
33
+ def copy_file(src: str, dst: str) -> bool:
34
+ """Copy file safely"""
35
+ try:
36
+ ensure_directory(str(Path(dst).parent))
37
+ shutil.copy2(src, dst)
38
+ return True
39
+ except Exception as e:
40
+ print(f"Copy error: {e}")
41
+ return False
42
+
43
+
44
+ def move_file(src: str, dst: str) -> bool:
45
+ """Move file safely"""
46
+ try:
47
+ ensure_directory(str(Path(dst).parent))
48
+ shutil.move(src, dst)
49
+ return True
50
+ except Exception as e:
51
+ print(f"Move error: {e}")
52
+ return False
53
+
54
+
55
+ def list_files(directory: str, extension: str = None) -> List[str]:
56
+ """List files in directory"""
57
+ path = Path(directory)
58
+
59
+ if not path.exists():
60
+ return []
61
+
62
+ if extension:
63
+ return [str(f) for f in path.glob(f"*{extension}")]
64
+ else:
65
+ return [str(f) for f in path.glob("*") if f.is_file()]