"Wind\[Wine]" "-6ren">
gpt4 book ai didi

python - 将字符串转换为 bash 转义形式

转载 作者:太空宇宙 更新时间:2023-11-04 09:19:23 28 4
gpt4 key购买 nike

如何将普通字符串(在我的例子中是文件名)转换为 Linux CLI/bash 转义字符串?

例子:

“今天天气真好”->“今天天气真好”

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

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

我想知道是否有更简单的方法来做到这一点,因为我正在我的 python 脚本中读取文件名,当我将它们转发给 bash 命令时,它失败了。

问题是文件的数量太多了,我不可能对它们进行重命名或字符串操作。


我在 CentOS 7 系统上使用 Python 2.7

最佳答案

使用shlex,参见here ,它有 escapeqoutesescapedqoutes 的参数。

关于python - 将字符串转换为 bash 转义形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43676064/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com