Python-Zstandard:如何解决错误:未定义全局名称“ zstd”?

时间:2018-08-08 14:27:01

标签: python-2.7

我尝试使用python 2.7在linux termianl中运行脚本。 我收到以下错误:

Traceback (most recent call last):
  File "main1.py", line 50, in <module>
    main()
  File "main1.py", line 25, in main
    ver = zstd.__version__;
NameError: global name 'zstd' is not defined

请检查代码并提出更改建议或成功运行脚本的任何其他方式。

# import zstd library for zstandard simple api access and Compress
import csv
import os
import time
import sys
import io
import random
import zlib
#import zstandard as zstd

from zstd import *

def main():

   #set path of INPUT FILE here
    path = 'sai.txt'
    fh_input = open(path, "rb")

    #determine size of input file
    sizeinfo_if = os.stat('sai.txt')
    print('Size of input file is :',sizeinfo_if.st_size,'Bytes')

    #make sure zstd is installed and running
    ver = zstd.__version__;
    print('Zstd version : ',ver)

    #File for already existing(or newly created ) output file where the compressed data needs to be written
    fh_output = open('output.txt','wb')

    #Zstd compressor object creation
    cctx = zstd.ZstdCompressor()
    initial_timestamp = time.time()

    #Get byte sized chucks from in File, compress and write to out file
    with open('sai.txt', 'rb') as fh_input:
      byte_chunk = fh_input.read(1)
      while byte_chunk:
        #bdata=bytes(str.encode(byte_chunk))
        compressed = cctx.compress(byte_chunk)
        with cctx.write_to(fh_output) as compressor:
          compressor.write(byte_chunk)
        byte_chunk = fh_input.read(1)
    end_timestamp = time.time()

    print('Time taken to compress:',end_timestamp - initial_timestamp)
    sizeinfo_of = os.stat('output.txt')
    print('Size of output File is:',sizeinfo_of.st_size,'Bytes')

    main()

0 个答案:

没有答案