Python - 将命令作为参数

标签 python

是否可以将命令作为参数传递给 Python,而不是将其写入文件中?

例如文件Hello.py的内容是:

=========================
print"Hello World!"
=========================

当我们这样做时:

$ Python Hello.py

它说:

Hello World!

但是是否可以直接将命令作为 Python 参数给出: 喜欢:

$python <Some Option> "print 'hello World!'"

最佳答案

您可以使用-c option :

Execute the Python code in command. command can be one or more statements separated by newlines, with significant leading whitespace as in normal module code.

示例:

python -c "print 'Hello World!'"

关于Python - 将命令作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21673992/

相关文章:

python - 在 Fabric 中以非详细模式添加 with 语句 (Python)

python - 将多个数据框导出为 html

python - 相当于 Win32::Process::Memory 的 python 进程内存搜索

python - 使用读取文件时减少 CPU 使用率

python - Django、Signals 和另一个进程

Python:检查关键字是否出现在字符串中

python - bool 标志字段的模式设计(数据库架构)

python - django-facebook 内部代码说找不到 facebook_id

python - 对 Django 应用程序的 celery 和 redis 进行故障排除

Python-删除同一索引处多个子列表的元素