问题
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