|
1
|
|
|
import os |
|
2
|
|
|
import datetime |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
class Filesystem(object): |
|
6
|
|
|
"""Wrapper of filesystem access functionality such as that implemented by |
|
7
|
|
|
the os package in the standard library. |
|
8
|
|
|
""" |
|
9
|
|
|
|
|
10
|
|
|
def __init__(self): |
|
11
|
|
|
self.open = open |
|
12
|
|
|
|
|
13
|
|
|
def fileExists(self, path): |
|
14
|
|
|
return os.path.isfile(path) |
|
15
|
|
|
|
|
16
|
|
|
def walk(self, path): |
|
17
|
|
|
return os.walk(path) |
|
18
|
|
|
|
|
19
|
|
|
def readlines(self, path): |
|
20
|
|
|
with open(path) as fhandle: |
|
21
|
|
|
lines = fhandle.read().splitlines() |
|
22
|
|
|
return lines |
|
23
|
|
|
|
|
24
|
|
|
def read(self, path): |
|
25
|
|
|
"""Read the contents of a textfile. |
|
26
|
|
|
|
|
27
|
|
|
Args: |
|
28
|
|
|
path: Path to the file to read. |
|
29
|
|
|
|
|
30
|
|
|
Returns: |
|
31
|
|
|
str: Contents of the file |
|
32
|
|
|
|
|
33
|
|
|
Raises: |
|
34
|
|
|
IOError: [Errno 2] No such file or directory: 'xyz' |
|
35
|
|
|
""" |
|
36
|
|
|
with open(path) as fhandle: |
|
37
|
|
|
contents = fhandle.read() |
|
38
|
|
|
return contents |
|
39
|
|
|
|
|
40
|
|
|
def write(self, path, content): |
|
41
|
|
|
"""Write string content to a textfile. |
|
42
|
|
|
|
|
43
|
|
|
Args: |
|
44
|
|
|
path: Path to the file to read. |
|
45
|
|
|
content (str): What to fill the file with |
|
46
|
|
|
""" |
|
47
|
|
|
with open(path, 'w') as fhandle: |
|
48
|
|
|
fhandle.write(content) |
|
49
|
|
|
|
|
50
|
|
|
def getsize(self, path): |
|
51
|
|
|
return os.path.getsize(path) |
|
52
|
|
|
|
|
53
|
|
|
def getctime(self, path): |
|
54
|
|
|
"""Get the creation time for the file at path. |
|
55
|
|
|
|
|
56
|
|
|
Args: |
|
57
|
|
|
path: Path to the file to read. |
|
58
|
|
|
|
|
59
|
|
|
Returns: |
|
60
|
|
|
datetime: Time when the file was last changed |
|
61
|
|
|
""" |
|
62
|
|
|
return datetime.datetime.fromtimestamp(os.path.getctime(path)) |
|
63
|
|
|
|
|
64
|
|
|
|