I want to copy a certain file to a specified path. This specified path has many hierarchies of directories which do not exist in advance and need to get created during copying.
I tried shutil.copy* functions but they all seem to require that the directory at destination path is pre-created.
Is there any functions that sets up these directories as required and copies the file?
Example usage:
copy_file('resources/foo.bar', expanduser('~/a/long/long/path/foo.bar'))
You can use
os.makedirsto recursively create the arborescence you need, then useshutil.copy.That way, you decompose the problem in manageable tasks (creation then copy), which lets you handle the case where the creation of the directories crashes (following the ‘explicit is better than implicit’).