Menu

[r7444]: / branches / mathtex / test / mplTest / path_utils.py  Maximize  Restore  History

Download this file

82 lines (65 with data), 2.5 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
71
72
73
74
75
76
77
78
79
#=======================================================================
""" A set of utilities for manipulating path information.
"""
#=======================================================================
import os
import shutil
import os.path
#=======================================================================
__all__ = [
'chdir',
'exists',
'extension',
'joinPath',
'mkdir',
'name',
'rm',
'rmdir',
'walk',
]
#-----------------------------------------------------------------------
def chdir( path ):
"""Change the current working directory to the specified directory."""
os.chdir( path )
#-----------------------------------------------------------------------
def exists( path ):
"""Returns true if the specified path exists."""
return os.path.exists( path )
#-----------------------------------------------------------------------
def extension( path ):
"""Returns the extension name of a filename."""
unused, ext = os.path.splitext( path )
return ext
#-----------------------------------------------------------------------
def joinPath( *args ):
"""Returns true if the specified path exists."""
return os.path.join( *args )
#-----------------------------------------------------------------------
def mkdir( path, mode = 0777, recursive = False ):
"""Create the specified directory."""
if recursive:
os.makedirs( path, mode )
else:
os.mkdir( path, mode )
#-----------------------------------------------------------------------
def name( path ):
"""Returns the name portion of a specified path."""
return os.path.basename( path )
#-----------------------------------------------------------------------
def rm( path ):
"""Remove the specified file."""
os.remove( path )
#-----------------------------------------------------------------------
def rmdir( path ):
"""Remove the specified directory."""
shutil.rmtree( path, ignore_errors = True )
#-----------------------------------------------------------------------
def walk( path ):
"""Recursively iterate over files and sub-directories."""
children = os.listdir( path )
children = [ os.path.join( path, child ) for child in children ]
for child in children:
yield child
if os.path.isdir( child ):
for grandchild in walk( child ):
yield grandchild
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.