2011-12-01 189 views
0
#!/usr/bin/python 
data = open("/home/mia/Desktop/results/all-nodup.txt", "r") 
fd = open("/home/mia/Desktop/results/all-filter.txt", "w") 

last_time = 0.0 
last_ip = None 
last_hash = None 
row = data.read() 

for line in row: 
     timestamp, ip, hash_value = line.split() 

     if ip==last_ip and hash_value==last_hash: 

      if float(timestamp) - float(last_time) >= 5.0: 
      fd.write("%s\t%s\t%s\n" % (str(timestamp), str(ip), str(hash_value))) 
      last_time, last_ip, last_hash = timestamp, ip, hash_value 
     else: 
      fd.write("%s\t%s\t%s\n" % (str(timestamp), str(ip), str(hash_value))) 
      last_time, last_ip, last_hash = timestamp, ip, hash_value 

fd.close() 

這是我整個的代碼,我去結果/目錄下運行:python filter.py 我得到一個錯誤信息:python: can't open file 'filter.py': [Errno 2] No such file or directoryPython不能打開文件

但其它腳本可以被執行,所以Python工作正常,也許我應該在這種情況下導入一些東西?

+2

不改變目錄的路徑,這是什麼'LS filter.py'說明了什麼?怎麼樣'head -1 filter.py'? – bukzor

回答

0

這是因爲filter.py不在目錄你在運行命令。

嘗試python /path/to/filter.py。您也可以使用相對路徑,例如,如果文件是一個目錄,則使用python ../filter.py

+0

filter.py在我運行命令的目錄中 – manxing

+0

@manxing:不,它不是 –

+0

你確定嗎? ... –

1

python甚至找不到你的filter.py腳本文件,所以改變你的代碼是沒用的。爲了解決這個問題你要麼需要:

  • filter.pyresults/目錄
  • 使用絕對路徑,例如python /path/to/script/filter.py
  • 找出正確的相對路徑,例如python ../../blah/filter.py
  • 放在哪裏filter.py駐留到PATH變量