-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_explorer.py
More file actions
70 lines (56 loc) · 1.84 KB
/
file_explorer.py
File metadata and controls
70 lines (56 loc) · 1.84 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
from pathlib import Path
import os
class FileExplorer:
"""Class to represent a full-fledged file explorer"""
def __init__(self):
self.currentDirectory = Path.home()
# FILE META
# method to get name of cwd
def cwdName(self):
return self.currentDirectory.name
# check if path is directory
def isPathDir(self, path):
return path.exists() and path.is_dir()
# get info about file
def getFileInfo(self, path: Path):
if path.exists():
return path.stat()
# method to list files in current directory
def cwdContents(self):
# check if current path is a directory
if not self.currentDirectory.is_dir():
return []
# list children
children = []
for child in self.currentDirectory.iterdir():
children.append(child)
return children
# NAVIGATION
# method to go 'home'
def home(self):
self.currentDirectory = Path.home()
# method to 'up' the directory hierarchy
def up(self, steps=1):
self.currentDirectory = self.currentDirectory.parent
# method to go 'down' the directory hierarchy
def down(self, destination):
for content in self.currentDirectory.iterdir():
if content.name == destination:
self.currentDirectory = content
return True
# no result found, return false
return False
# go to an arbitrary directory
def goToDir(self, target):
# check if target is absolute
if target.exists() and target.is_dir():
self.currentDirectory = target
# MISC
def getFileRep(self, file):
fileInfo = os.stat(file)
fileRep = {
'name': file.name,
'is_dir': file.is_dir(),
'size': fileInfo.st_size
}
return fileRep