33726

Special characters in OSX filename ? (Python os.rename)

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

Answer1:

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 :.

Answer2:

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.

Recommend

  • R Rvest for() and Error server error: (503) Service Unavailable
  • Problem creating bioclimatic variables using dismo packages
  • SSH into Specified Directory by Default?
  • Protected worksheet allows editing cell format by copy and paste
  • Android - Multiple GridViews in a ListView
  • How do I use a method I have made? JAVA
  • Setting each tab on TabBarController a different colour when selected?
  • Grouping data on column value
  • How can I determine file encodings on Windows / IIS?
  • Converting/writing a BufferedImage to postscript
  • Google cloud speech syncrecognize “INVALID_ARGUMENT”
  • Which video formats to use for Java ME in mobile phones?
  • Insert Path of a file with \\\\ in mysql using java
  • Find files in current directory sorted by modified time and store result in an array
  • pandas parse csv with left and right quote chars
  • How do I include superscripts in NSString?
  • preg_replace
  • How to emulate integrated numeric keypad cursor keys in linux
  • HSQLDB Statement and Java NaN doubles
  • Delphi. Analog of Memo/RichEdit
  • How to remove last utf8 char of a python string
  • Passing information to server-side function in a Google Docs Add On
  • How to change placeholder text in an autocomplete activity of android google place?
  • Updating Dojo provide
  • Yii2: Finding file and getting path in a directory tree
  • ASP.NET MVC Application won't update some controllers
  • how to upload multiple files in c# windows application
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • How do I open a C file with a relative path?
  • Jquery UI tool tip close icon
  • Highlight and Bold text in JTextPane
  • swift auto completion not working in Xcode6-Beta
  • SSO with signing and signature validation doesn't work
  • How to set my toolbar fixed while scrolling android
  • How to make Safari send if-modified-since header?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • costura.fody for a dll that references another dll