diff options
author | Jack Jansen <jack.jansen@cwi.nl> | 1995-08-31 13:40:03 (GMT) |
---|---|---|
committer | Jack Jansen <jack.jansen@cwi.nl> | 1995-08-31 13:40:03 (GMT) |
commit | 423c798b3c769c901e8a342a5a85e4612277912a (patch) | |
tree | 9513be4945e8c32e3f798c83ec51b7bb4c2dbd3e /Mac | |
parent | f5101ee5dc74a6f774642217459b062fe295e2c6 (diff) | |
download | cpython-423c798b3c769c901e8a342a5a85e4612277912a.zip cpython-423c798b3c769c901e8a342a5a85e4612277912a.tar.gz cpython-423c798b3c769c901e8a342a5a85e4612277912a.tar.bz2 |
copy() can now create destination path
Diffstat (limited to 'Mac')
-rw-r--r-- | Mac/Lib/macostools.py | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/Mac/Lib/macostools.py b/Mac/Lib/macostools.py index 309f960..87eff6e 100644 --- a/Mac/Lib/macostools.py +++ b/Mac/Lib/macostools.py @@ -35,13 +35,27 @@ def mkalias(src, dst): dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag dstfss.SetFInfo(dstfinfo) -def copy(src, dst): +def mkdirs(dst): + """Make directories leading to 'dst' if they don't exist yet""" + if dst == '' or os.path.exists(dst): + return + head, tail = os.path.split(dst) + print 'XX', dst, '->', (head, tail) + # XXXX Is this a bug in os.path.split? + if not ':' in head: + head = head + ':' + mkdirs(head) + os.mkdir(dst, 0777) + +def copy(src, dst, createpath=0): """Copy a file, including finder info, resource fork, etc""" + if createpath: + mkdirs(os.path.split(dst)[0]) srcfss = macfs.FSSpec(src) dstfss = macfs.FSSpec(dst) - ifp = fopen(srcfss.as_pathname(), 'rb') - ofp = fopen(dstfss.as_pathname(), 'wb') + ifp = open(srcfss.as_pathname(), 'rb') + ofp = open(dstfss.as_pathname(), 'wb') d = ifp.read(BUFSIZ) while d: ofp.write(d) @@ -49,8 +63,8 @@ def copy(src, dst): ifp.close() ofp.close() - ifp = fopen(srcfss.as_pathname(), '*rb') - ofp = fopen(dstfss.as_pathname(), '*wb') + ifp = open(srcfss.as_pathname(), '*rb') + ofp = open(dstfss.as_pathname(), '*wb') d = ifp.read(BUFSIZ) while d: ofp.write(d) @@ -66,12 +80,9 @@ def copy(src, dst): def copytree(src, dst): """Copy a complete file tree to a new destination""" if os.path.isdir(src): - if not os.path.exists(dst): - os.mkdir(dst) - elif not os.path.isdir(dst): - raise Error, 'Not a directory: '+dst + mkdirs(dst) files = os.listdir(src) for f in files: copytree(os.path.join(src, f), os.path.join(dst, f)) else: - copy(src, dst) + copy(src, dst, 1) |