python - recursive file copying into subdirectory -
i need copy files , folders current folder subdirectory. best way so? tried following snippet fails fails if destination directory exists.
def copy(d=os.path.curdir): dest = "t" in os.listdir(d): if os.path.isdir(i): shutil.copytree(i, dest) else: shutil.copy(i, dest)
i have feeling same task can done in better , easier manner. how do it?
i never on python, following solution came mind. doesn't simple, should work , can simplified (haven't checked, sorry, no access computer now):
def copydirectorytree(directory, destination, preservesymlinks=true): entry in os.listdir(directory): entrypath = os.path.join(directory, entry) if os.path.isdir(entrypath): entrydest = os.path.join(destination, entry) if os.path.exists(entrydest): if not os.path.isdir(entrydest): raise ioerror("failed copy thee, destination `" + entrypath + "' directory exists , not directory") copydirectorytree(entrypath, entrydest, preservesymlinks) else: shutil.copytree(entrypath, entrydest, preservesymlinks) else: #symlinks , files if preservesymlinks: shutil.copy(entrypath, directory) else: shutil.copy(os.path.realpath(entrypath), directory)
Comments
Post a Comment