为什么python不能识别shell赋值!语法

2024-09-28 23:45:50 发布

您现在位置:Python中文网/ 问答频道 /正文

Python、python2或python3,无论我使用哪种命令,简单的shall命令(例如cat、head)都不起作用

>>> !cat testhts.txt
  File "<stdin>", line 1
    !cat testhts.txt
    ^
SyntaxError: invalid syntax

可能我错过了一些非常简单的东西,但我花了太多的时间来保持自己寻找。 提前谢谢你的帮助


Tags: 命令txtstdinline时间headpython3cat
1条回答
网友
1楼 · 发布于 2024-09-28 23:45:50

如果要在python中执行shell代码,可以使用subprocess模块

import subprocess
subprocess.call(' '.join(['cat','testhts.txt']),shell=True)

正如约瑟夫所说,使用!只适用于jupyter笔记本

相关问题 更多 >