OSX 文件名中的特殊字符? (Python os.rename)
我正在尝试使用 python 脚本在 OSX 上自动重命名一些文件。但我无法使用正斜杠等特殊字符:
oldname = "/test"
newname = "/test(1\/10)"
os.rename(oldname, newname)
我认为我确实有编码问题。但使用 re.escape 或使用 UTF-8 unicode 编码的不同尝试对我来说并没有成功。你能给点提示吗?
谢谢! 马可
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
大多数文件系统的共同点是它们不允许在文件名中使用目录分隔符(斜杠)。
也就是说,在 Mac OS X 中,您可以在 finder 中出现带斜杠的文件名,您可以尝试用
:
替换斜杠。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
:
.如果您尝试重命名文件夹“/test”,则需要以 root 身份运行 python,否则您将无权更改根目录中的内容。此外,新名称中的斜杠将不起作用,因为 python 将尝试查找目录“/test(1”,因此您必须放弃目录分隔符。另外,来自 python 文档的内容可能会有所帮助。
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.