views:

417

answers:

2

I am trying to rename some files automatically on OSX with a python script. But I fail to work with special characters like forward slash etc.:

oldname = "/test"
newname = "/test(1\/10)"
os.rename(oldname, newname)

I think I do have an encoding problem. But different tries with re.escape or using UTF-8 unicode encodings havent been successful for me. Would you have a hint?

Thanks! Marco

+1  A: 

What most of the file systems have in common is that they do not allow directory separators (slashes) in filenames.

That said, in Mac OS X you can have file names appear with slashes in finder, you can try replacing slashes with :.

Michael Krelin - hacker
thanks, replacing forward slashes with colons results in the desired output. And it is rendered visually in Finder as if I used a forward slash. And colon directly is forbidden in a OSX file name? Is there any function that does the transformation (including maybe other tricky characters?
marco4net
Yes, colon is forbidden. No idea about the function even less about the python function.
Michael Krelin - hacker
ok, a better python functionality would be nice, but that'll do it! Thanks! That might be relevant too: http://stackoverflow.com/questions/1033424/how-to-remove-bad-path-characters-in-python
marco4net
It's not really Python's fault: the *real* path separator in OS X is the slash. It's only a Finder display hack that makes `:` misleadingly appear as `/`; on the command line you'll see the real character. This oddity is presumably to satisfy old-school Mac users who used to be able to use `/` in filenames because the pre-OS X path separator used to be the colon.
bobince
Yes, it has absolutely nothing to do with python. I know nearly nothing about python yet I was able to answer the question ;-)
Michael Krelin - hacker
A: 

If you're trying to rename the folder '/test' you'll need to run python as root, otherwise you won't have privileges to change stuff in the root. Furthermore the slash in your new name won't work as python will try find a directory "/test(1", so you'll have to let the directory separator go. Also this from the python documentation might be helpful.

Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). On Windows, if dst already exists, OSError will be raised even if it is a file; there may be no way to implement an atomic rename when dst names an existing file. Availability: Unix, Windows.

Matti
python will try? Is python that bad? Wouldn't it pass it to syscall as is and let OS fail?
Michael Krelin - hacker
for sake of simplicity, I used that file in root, but I get OSError: [Errno 2] No such file or directory. Thats why I hoped more for a encoding/escaping function that I could use.
marco4net