2012-09-13 158 views
2

我寫了一個python腳本來查詢freebase一個開源數據庫。我在windows中編寫文件並將其移植到linux。我已經改變了該文件的權限,並添加相應的頭還沒有在Linux shell收益爲:python文件返回爲沒有這樣的文件或目錄

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()  
+3

什麼給你'沒有這樣的文件或目錄'? –

+3

檢查文件系統上是否實際上有/ usr/bin/env –

+3

腳本有幾個語法錯誤(例如'null'),所以它必須是解釋器。 –

回答

1

如果在/ usr/bin中/ env的是從你的Linux失蹤系統,您將收到此錯誤:

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

(其中test.py是腳本的名稱)

如果蟒蛇丟失(這不應該是,大多數Linux系統依賴於它的這些天),您將獲得:

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

有沒有在python腳本本身,我可以看到,這將使你任何其他類似的錯誤,所以我懷疑這是其中的一個。

-1

運行它想:

python /path/to/file.py 

然後你就可以工作,休息出來以後。

1

在shell中運行以下命令:

$其中蟒蛇

變化一線

#!在/ usr /斌/包膜蟒蛇

#!_ output_of_which_python_

6

我碰到同樣的問題,最近又傳出。問題是移植文件的文件格式仍然是DOS格式並且包含特殊字符。對腳本文件命令運行dos2unix解決了問題。

+0

這爲我做了詭計。 – AndersTornkvist

相關問題