简体   繁体   English

Python subprocess.check_output转换为Windows

[英]Python subprocess.check_output conver to windows

I wrote some code that works fine on a Linux machine but does not run on 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()

I'm getting an error: FileNotFoundError: [WinError 2] The system cannot find the file specified 我收到错误消息:FileNotFoundError:[WinError 2]系统找不到指定的文件

When I check it it with os.path.exists(file_path), it returns true. 当我用os.path.exists(file_path)检查它时,它返回true。 Any advice on how to modify this code so that it works on both Python 2 & 3 plus Windows and Linux would be appreciated. 任何有关如何修改此代码以使其在Python 2和3以及Windows和Linux上均可使用的建议将不胜感激。

this message only means one thing: the executable could not be found. 此消息仅表示一件事:找不到可执行文件。

this has nothing to do with your data file, since the process isn't even run. 这与您的数据文件无关,因为该过程甚至没有运行。

And why that? 那为什么呢? because while zgrep is standard on Linux, it's a third party port on Windows, so you have to install it first from here 因为尽管zgrep在Linux上是标准端口,但在Windows上却是第三方端口,因此您必须首先从此处安装

Note that if you only want to grep a string on csv files, it's overkill to use zgrep . 请注意,如果您只想在csv文件上grep一个字符串,那么使用zgrep It's much better to use a native python approach, reading lines (or rows, using the csv module) and matching patterns. 最好使用本机python方法,读取行(或行,使用csv模块)并匹配模式。 You can even open .gz files natively. 您甚至可以本地打开.gz文件。 Then it will really be portable. 然后它将真正是便携式的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM