blob: e4c117c363c95a57c96521923cc5d497b38e57a3 (
plain)
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
|
import os
import errno
def mkdir_p(path):
"""same as action of the unix command
mkdir -p
"""
try:
os.makedirs(path)
except OSError, e:
if e.errno == errno.EEXIST:
pass
else:
raise e
def extend_file_name(file_path, extension):
"""append file extension to a string that
represents file name, if the string does not
already have that same extension"""
if not file_path.endswith(extension):
file_path += extension
return file_path
def create_file_if_does_not_exist(file_path, print_warning = True):
"""if file at file_path does not exist, create it and return
the file object, otherwise return None"""
if not os.path.exists(file_path):
return open(file_path, 'w+')
else:
if print_warning:
print "File %s exists" % file_path
return None
|