Convert strings to bash escaped form

ぐ巨炮叔叔 提交于 2019-12-25 11:51:41

问题


How to make normal strings (filenames in my case) to Linux CLI/bash escaped strings?

Examples:

"It's a great weather today" -> "It\'s\ a\ great\ weather\ today"

"Wind [Wine]" -> "Wind\ [Wine]"

"/Downloads/RPM's/" -> "/Downloads/RPM\'s/"

I would like to know if there's an easier way to do that, as I am reading filenames in my python script and when I am forwarding them to a bash command, it's failing.

The problem is the number of files are too many and it won't be possible for me to rename or do string manipulation for them.


I am using Python 2.7 on a CentOS 7 System


回答1:


Use shlex, see here, it has arguments for escape, qoutes and escapedqoutes.



来源:https://stackoverflow.com/questions/43676064/convert-strings-to-bash-escaped-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!