Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么python不能识别具有的shell分配!语法_Python_Shell - Fatal编程技术网

为什么python不能识别具有的shell分配!语法

为什么python不能识别具有的shell分配!语法,python,shell,Python,Shell,Python、python2或python3,无论我使用哪一种,简单的shall命令(例如cat、head)都不起作用 >>> !cat testhts.txt File "<stdin>", line 1 !cat testhts.txt ^ SyntaxError: invalid syntax >>!cat testhts.txt 文件“”,第1行 !cat testhts.txt ^ SyntaxError:无效语法 也许我错过了一些

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

>>> !cat testhts.txt
  File "<stdin>", line 1
    !cat testhts.txt
    ^
SyntaxError: invalid syntax
>>!cat testhts.txt
文件“”,第1行
!cat testhts.txt
^
SyntaxError:无效语法

也许我错过了一些非常简单的东西,但我花了太多的时间独自寻找。
提前谢谢你的帮助

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

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

正如约瑟夫提到的,使用!仅适用于jupyter笔记本电脑。

您凭什么认为这会起作用?这只适用于iPython(jupyter)内核。为什么python会使用!语法?(或者任何shell语法/命令)“可能我遗漏了一些非常简单的东西”=>是的,确实如此:pythonshell只执行Python代码-带前缀的命令是iPython/jupyter的特性,而不是Python语言。