[英]Python subprocess.check_output conver to windows
我寫了一些在Linux機器上可以正常運行但不能在Windows上運行的代碼。
import subprocess
import pandas as pd
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
def zgrep_data(f, string='', index='TIMESTAMP'):
if string == '':
out = subprocess.check_output(['zgrep', string, f])
grep_data = StringIO(out)
data= pd.read_csv(grep_data, sep=',', header=0)
else:
col_out = subprocess.check_output(['zgrep', index, f])
col_data = StringIO(col_out)
columns = list(pd.read_csv(col_data, sep=','))
out = subprocess.check_output(['zgrep', string, f])
grep_data = StringIO(out)
data= pd.read_csv(grep_data, sep=',',names=columns, header=None)
return data.set_index(index).reset_index()
我收到錯誤消息:FileNotFoundError:[WinError 2]系統找不到指定的文件
當我用os.path.exists(file_path)檢查它時,它返回true。 任何有關如何修改此代碼以使其在Python 2和3以及Windows和Linux上均可使用的建議將不勝感激。
此消息僅表示一件事:找不到可執行文件。
這與您的數據文件無關,因為該過程甚至沒有運行。
那為什么呢? 因為盡管zgrep
在Linux上是標准端口,但在Windows上卻是第三方端口,因此您必須首先從此處安裝
請注意,如果您只想在csv文件上grep一個字符串,那么使用zgrep
。 最好使用本機python方法,讀取行(或行,使用csv
模塊)並匹配模式。 您甚至可以本地打開.gz
文件。 然后它將真正是便攜式的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.