-
Notifications
You must be signed in to change notification settings - Fork 0
/
filehelpers.py
64 lines (54 loc) · 1.76 KB
/
filehelpers.py
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
# coding=utf-8
import os
import codecs
import shutil
def ensureDirectory(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def ensureNoFile(file):
if os.path.isfile(file):
os.remove(file)
def ensureEmptyDir(directory):
"""
Ensure that the directory exists but that is empty.
If it does not exist, create it.
If it exist remove everything in it.
:param directory: The directory to create/empty
"""
# http://stackoverflow.com/questions/185936/delete-folder-contents-in-python
if os.path.isdir(directory):
for file_object in os.listdir(directory):
file_object_path = os.path.join(directory, file_object)
if os.path.isfile(file_object_path):
os.unlink(file_object_path)
else:
shutil.rmtree(file_object_path)
# TODO: this will fail for symbolic link
else:
ensureDirectory(directory)
def touchFile(file, times=None):
with open(file, 'a'):
os.utime(file, times)
def fileContent(file):
with codecs.open(file, 'r', 'utf-8') as f:
content = f.read()
return content
def saveContent(file, content):
with codecs.open(file, "w", "utf-8") as f:
f.write(content)
def directoryItemPaths(directory, predicate=os.path.isdir):
_ = []
for name in os.listdir(directory):
path = os.path.join(directory,name)
if predicate(path):
_.append(path)
return _
def generateFile(inputFilename, outputFilename, str2strFun):
input = fileContent(inputFilename)
output = str2strFun(input)
saveContent(outputFilename, output)
def removeTrailingSeparator(filename):
if filename.endswith(os.sep):
return filename[:-1]
else:
return filename