[Tutor] moving files from one dir to another

Kent Johnson kent37 at tds.net
Tue May 12 01:38:00 CEST 2009


On Mon, May 11, 2009 at 4:22 PM, Matt Herzog <msh at blisses.org> wrote:
> Should be simple, right? Not for me, heh.
>
> def schmove(src,dst):
> ...         src = '/home/datasvcs/PIG/cjomeda_exp/'
> ...         dst = '/home/datasvcs/PIG/cjomeda_exp_archive/'
> ...         listOfFiles = os.listdir(src)
> ...         for filez in listOfFiles:
> ...             os.system("mv"+ " " + src + " " + dst)

You aren't using filez at all. Try
  os.system("mv %s/%s %s" % (src, filez, dst))

or you can also use os.path.join() to construct the source path.

> The above code does not copy anything. I sure wish it would spit just one error.
>
> I sure wish I could for the last line go,
>
> shutil.move("src", "dest")

should be
  shutil.move(os.path.join(src, filez), dst)

Kent


More information about the Tutor mailing list