通过终端使用import os命令给出错误

时间:2019-11-23 11:17:32

标签: python bash

我在尝试使用import os命令通过终端实现反响时遇到此错误:

$ import os
$ 
$ search_list = "./emails.list"
bash: search_list: command not found
$ search_directory = "leaks"
bash: search_directory: command not found
$ 
$ def find_in_files(rootdir, search_str):
bash: syntax error near unexpected token `('
$     for folder, dirs, files in os.walk(rootdir):
bash: syntax error near unexpected token `dirs,'
$         for file in files:
>             fullpath = os.path.join(folder, file)
bash: syntax error near unexpected token `fullpath'
$             with open(fullpath, 'r') as f:
bash: syntax error near unexpected token `('
$                 line_num=0
$                 for line in f:
>                     line_num=+1
bash: syntax error near unexpected token `line_num=+1'
$                     if search_str in line:
>                         print(line_num, fullpath, search_str)
bash: syntax error near unexpected token `line_num,'
$ 
$ 
$ email_list = open(search_list).read()
bash: syntax error near unexpected token `('
$ for email in email_list.split("\n"):
bash: syntax error near unexpected token `('
$     find_in_files(search_directory, email)

1 个答案:

答案 0 :(得分:0)

您的shell(bash)不了解python。您需要首先启动python解释器。可以通过两种方式完成此操作,具体取决于您要执行的操作:

$ python3
>>> # paste here your code

或者简单地:

$ python3 -c '
> # paste here your code
'

请注意,如果代码中包含字符',则必须处理转义符。

使用 text 编辑器将代码写入文件并使用以下命令运行起来更简单:

$ python3 myfile.py

顺便说一句,bash有一个import命令,但与import在Python中的操作无关:

  

NAME          import-将任何可见窗口保存在X服务器上,并将其输出为          图像文件。您可以捕获单个窗口,整个屏幕或任何一个          屏幕的矩形部分。

因此,您可能保存了一个os文件,其中包含屏幕截图。