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

Lukáš Lalinský
  • 40,587
  • 6
  • 104
  • 126
marco4net
  • 175
  • 1
  • 3
  • 10

2 Answers2

2

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
  • 138,757
  • 24
  • 193
  • 173
  • 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 Dec 13 '09 at 13:38
  • Yes, colon is forbidden. No idea about the function even less about the python function. – Michael Krelin - hacker Dec 13 '09 at 13:50
  • 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 Dec 13 '09 at 13:55
  • 1
    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 Dec 13 '09 at 14:51
  • 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 Dec 13 '09 at 14:54
  • actually you can create a filename (likely using python per yt-downloader (yt-dlp)) that has forward slash there. You can even copy the files and move around. Until you try to upload to dropbox it was refused. – Dennis Ng Aug 08 '23 at 21:47
0

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 Lyra
  • 12,828
  • 8
  • 49
  • 67
  • python will try? Is python that bad? Wouldn't it pass it to syscall as is and let OS fail? – Michael Krelin - hacker Dec 13 '09 at 13:14
  • 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 Dec 13 '09 at 13:40