python文件不返回这样的文件或目录

2024-09-28 18:59:18 发布

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

我编写了一个python脚本来查询一个开源数据库freebase。我在windows中编写了这个文件并将其移植到linux。我已经更改了对该文件的权限并添加了适当的头,但是linux shell返回如下:

No such file or directory

这是文件:

#! /usr/bin/env python
import urllib
import string

#query freebase to find results containing graduates from the University of Texas

query1=[{
  "name": null,
  "type": "/people/person",
  "!/education/education/student": [{
    "!/education/educational_institution/students_graduates": [{
      "id": "/en/university_of_texas"
    }]
  }]
}]

query2=[{
  "id": "/en/university_of_texas",
  "/education/educational_institution/students_graduates": [{
    "student": {
      "name": null
    },
        "degree": {
      "name": null
    },
    "end_date": null,
    "major_field_of_study": [{
      "name": null
    }]
  }]
}]

html = urllib.urlopen("https://www.googleapis.com/freebase/v1/mqlread?query="+query2)

library = json.loads(html)

name_dic = {}

for e in library["result"]:

    name_dic[e["student"]["name"]] = [e["degree"]["name"],int(e["end_date"]),e["major_field_of_study"][0]["name"]]

conn = sqlite3.connect('data.db')
c = conn.cursor()
t=[]

for key in name_dic.iterkeys():
    t.append((key, name_dic[key][0],name_dic[key][1],name_dic[key][2]))
try:
    c.executemany('insert into people values(?,?,?,?)',t)
    print "entities found and saved"
except sqlite3.IntegrityError:
    for k in t:
        try:
            c.execute('insert into people values (?,?,?,?)',k)
            print (str(k[0])+" was added")
        except sqlite3.IntegrityError:
            print "Could not save entities"
conn.commit()       

Tags: 文件ofkeynameinforconnpeople
3条回答

在shell上运行以下命令:

$哪条Python

更改第一行

#!/usr/bin/envPython

#!_输出哪个python

如果Linux系统中缺少/usr/bin/env,则会出现以下错误:

-bash: ./test.py: /usr/bin/env: bad interpreter: No such file or directory

(其中test.py是脚本的名称)

如果缺少python(现在大多数Linux系统都依赖python),您将得到:

/usr/bin/env: python: No such file or directory

在python脚本本身中,我看不到任何其他类似的错误,所以我怀疑它是这两个错误之一。

我最近遇到了同样的问题。问题是,移植文件的文件格式仍然是dos格式,并且包含特殊字符。在script file命令上运行dos2unix解决了这个问题。

相关问题 更多 >