[Tutor] Move all files to top-level directory

Dotan Cohen dotancohen at gmail.com
Mon Apr 12 18:28:00 CEST 2010


I'm really stuck here. I need move all files in subdirectories of cwd
to cwd. So that, for instance, if we are in ~/photos then this file:
~/photos/a/b/file with space.jpg
...will move to this location:
~/photos/file with space.jpg

This is what I've come up with:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
currentDir = os.getcwd()

files = os.walk(currentDir)
for f in files:
    os.rename(f, currentDir)


However, it fails like this:
$ ./moveUp.py
Traceback (most recent call last):
  File "./moveUp.py", line 8, in <module>
    os.rename(f, currentDir)
TypeError: coercing to Unicode: need string or buffer, tuple found

I can't google my way out of this one! The filenames on my test setup
are currently ascii, but this does need to be unicode-compatible as
the real filenames will have unicode non-ascii characters and even
spaces in the filenames!

What should I be reading now?

Thanks!


-- 
Dotan Cohen

http://bido.com
http://what-is-what.com


More information about the Tutor mailing list