8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
from string import Template
class cd:
"Context manager for changing the current working directory"
def __init__ (self, newPath):
self.newPath = os.path.expanduser(newPath)
def __enter__ (self):
self.savedPath = os.getcwd()
os.chdir(self.newPath)
def __exit__ (self, etype, value, traceback):
os.chdir(self.savedPath)
|
>
|
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
from string import Template
class cd:
"Context manager for changing the current working directory"
def __init__ (self, newPath):
self.newPath = os.path.expanduser(newPath)
self.savedPath = ""
def __enter__ (self):
self.savedPath = os.getcwd()
os.chdir(self.newPath)
def __exit__ (self, etype, value, traceback):
os.chdir(self.savedPath)
|
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
for sf in os.listdir(sp):
spf = os.path.join(sp, sf)
try:
if os.path.isfile(spf):
os.unlink(spf)
elif os.path.isdir(spf):
shutil.rmtree(spf)
except Exception as e:
print(e)
def createCleanFolder (sp):
"make an empty folder or erase its content if not empty"
if not os.path.exists(sp):
os.makedirs(sp, exist_ok=True)
|
|
|
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
for sf in os.listdir(sp):
spf = os.path.join(sp, sf)
try:
if os.path.isfile(spf):
os.unlink(spf)
elif os.path.isdir(spf):
shutil.rmtree(spf)
except (OSError, shutil.Error) as e:
print(e)
def createCleanFolder (sp):
"make an empty folder or erase its content if not empty"
if not os.path.exists(sp):
os.makedirs(sp, exist_ok=True)
|