[Tutor] Move files to a new directory with matching folder names

questions anon questions.anon at gmail.com
Thu May 22 02:25:27 CEST 2014


I have files contained separately in folders labelled by years and months
and I would like to copy them into another directory where they have
matching folder names of years and months.
I can do this for one folder at a time (see below) but I want it to be able
to go through all the folders (2002/01,2002/02.....2012/11,2012/12) and
copy files from one directory to another directory with matching subdir
names.
thanks

import os

import shutil

source="/Data/allclimatedata/2002/01"

destination="/Data/rainfall/2002/01"



for file in os.listdir(source):

path = os.path.join(source, file)

shutil.copy(path, destination)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140522/f40f1cbd/attachment.html>


More information about the Tutor mailing list