[Tutor] Moving all files in a directory

Bradford R. Bowman bowman at mazirian.com
Thu Jul 21 05:56:14 CEST 2005

On Wed, 2005-07-20 at 20:22 -0400, Bradford R. Bowman wrote:
> I know this should be easy, but how would I go about moving all files
> contained in one directory into another directory (as in the shell
> command "mv /path/to/dir1/* /path/to/dir2")?  Is the os module's rename
> function used for this?

I ended up with this function:

def movefiles(dir1, dir2):
  """Moves all files in dir1 to dir2"""
    contents = os.listdir(dir1)
    for file in contents:
      old = os.path.join(dir1, file)
      new = os.path.join(dir2, file)
      os.rename(old, new)
It seems to work, but this looks inelegant.  Does anyone have any better

Bradford R. Bowman
GnuPG Public Key available at:

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://mail.python.org/pipermail/tutor/attachments/20050720/410df76c/attachment.pgp

More information about the Tutor mailing list