python - 如何引用脚本以使单引号不消失?
问题描述
我想使用 python 脚本在文件中添加一些单词。这是我的脚本(名称为test.py
)以及我如何执行脚本。
import os
some_python_words = "sys.path.insert(0, os.path.abspath('..'))\n"
scripts = "echo '%s' >> 'conf.py' " % (some_python_words)
os.system(scripts)
正如我在终端中执行的那样python test.py
。我的文件中写的是
sys.path.insert(0, os.path.abspath(..))
的单引号没''
了..
。我应该怎么办?
解决方案
这是因为在 shell 中处理了单引号和双引号。单引号字符串不能有内部单引号,但双引号可以。请参阅Bash 中单引号和双引号之间的区别以获得出色的文章。
只需在创建 shell 字符串时更改您的引用
scripts = """echo "%s" >> 'conf.py' """ % (some_python_words)
推荐阅读
- swift - AVPlayerItem 的 selectedMediaOption(in:) 与 seek(to:) 调用后的可用选择选项不一致
- php - 'key' meta_query 不起作用自定义高级搜索(WordPress)
- php - 将特定价格添加到产品 Prestashop
- javascript - 无法使用 D3 强制布局显示文本标签
- javascript - 限制表选择不起作用 - 材质 UI 表 - 反应
- ruby-on-rails - 在 High Sierra 上安装 Mysql 5.7
- node.js - 用于链接多个 http.requests 的 ASYNC/AWAIT
- json - 如何将 JSON 文档导入 Cloudant NoSQL DB
- c# - 为什么 Visual Studio IIS Express 尝试使用 ASPNetCore 模块运行 asp.net 完整框架应用程序?
- angular - 其他变量变化上的变量变化