Skip to content Skip to sidebar Skip to footer

Process A Set Of Files From A Source Directory To A Destination Directory In Python

Being completely new in python I'm trying to run a command over a set of files in python. The command requires both source and destination file (I'm actually using imagemagick conv

Solution 1:

Change your loop to:

for root, dirs, files inos.walk(srcdir):
    destroot = os.path.join(destdir, root[len(srcdir):])
    for adir in dirs:
        os.makedirs(os.path.join(destroot, adir))
    for filename in files:
        sourceFile = os.path.join(root, filename)
        destFile = os.path.join(destroot, filename)
        processFile(sourceFile, destFile)

Solution 2:

There are a few relative path scripts out there that will do what you want -- namely find the relative path between two paths. E.g.:

Unfortunately, I don't think this functionality has ever been added to core python.

Solution 3:

While not pretty, this will preserve the directory structure of the tree:

_, _, subdirs = root.partition(srcdir)
destfile = os.path.join(destdir, subdirs[1:], filename)

Post a Comment for "Process A Set Of Files From A Source Directory To A Destination Directory In Python"