python - 如何在Python中获取日志文件中的运行命令

标签 python

如何在Python中获取日志文件中的运行命令?

我已在命令行中运行以下命令,并希望在日志文件中打印相同的命令,以便我可以使用日志文件中的该命令并再次运行它。

示例: python checkcelllist -c celllist -s ronak.lib

它应该在日志文件中打印以上命令。

python checkcelllist -c celllist -s ronak.lib

最佳答案

您可以将 sys.argv 的内容转储到文件中:

print >> logfile, ' '.join(sys.argv)

请注意,这不包括解释器 (python),仅包括脚本名称和参数。

如果您的参数可以包含空格:

print >> logfile, ' '.join([(repr(arg) if ' ' in arg else arg) for arg in sys.argv])

关于python - 如何在Python中获取日志文件中的运行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18974705/

相关文章:

python - uwsgi + nginx + pypy Web 服务基准测试

python - 暴力破解python

python - Sphinx 与 Python 函数交叉引用的问题

python - 避免 Python 代码 redux 中的代码重复

Python SQLite3 - 表输出格式问题和处理 IntegrityError

python - 列表之间不需要的空行

c# - IronPython 无法导入模块 os

python - 使用 numpy magic 避免三重嵌套 for 循环

python - sqlalchemy.exc.OperationalError : (sqlite3. OperationalError) 未知数据库 "mydb"

python - 使用 BeautifulSoup 从 HTML 创建 JSON 结构