Philip Caputo
Philip Caputo

Reputation: 50

file is not a zip file error but I am not opening a zip file

I am building a BCT trading bot. I am pulling data in from the API from my exchange. I am saving the data in files like btc_10Xave_timestamp(). I have another script that is analyzing this data. These files are produced every 100 trades regardless of trade size. Every so often I combine them into a master file of data. The issue that I am having is with this analyzer.

Here is my code:

import numpy as np
import pandas as pd
import glob, time
from playsound import playsound

print('Monitoring for time to trade...')
while True:
    try:
        previous_time = 0.0
        time_of_file = 0.0
        sum_size = 0.0
        last_price = 0.0
        previous_price = 0.0
        files_array = np.sort(glob.glob('btc*.xlsx'))

        if len(glob.glob('btc*.xlsx')) > 1:

            df = pd.read_excel(files_array[-1], header=None)
            sum_size = df.iloc[:,2].sum()
            last_price = df.iloc[-1, 1]

            previous_df = pd.read_excel(files_array[-2], header=None)
            previous_price = previous_df.iloc[-1, 1]

            diff_price = last_price - previous_price

            previous_time = files_array[-2]    [11:files_array[0].index('.xlsx')-1]

            last_time = files_array[-1][11:files_array[0].index('.xlsx')-1]
            time_lapse = str(float(last_time) - float(previous_time))


            if float(time_lapse) < 10:
                #volumne is rapidly moving in at this point
                if diff_price > 0:
                    #buying opp
                    playsound('golong.mp3')
                    print('Buying Opportunity Here @: $'+   str(last_time))
                    print("Last 100 trades of : " + str(sum_size) + " BTC in " + time_lapse + " seconds.")
                    print("This volumne moved the market " +str(diff_price) + " USD")
                elif diff_price < 0:
                    #sell opp
                    playsound('sellit.mp3')
                    print('Selling Opportunity Here @: $' + str(last_time))
                    print("Last 100 trades of : " + str(sum_size) + " BTC in " + time_lapse + " seconds.")
                    print("This volumne moved the market " +str(diff_price) + " USD")
                else:
                    pass
        time.sleep(10)

    except Exception as e:
        print(e)
        continue

Again the error that is being thrown says: "file is not a zip file" I am totally confused as to how or why this happening.
From the appearance of this code it is simple enough. I have no idea where the source of error is. Any insight is of course appreciated. Thank you to all you awesome coders out there!

Upvotes: 1

Views: 245

Answers (1)

Thomas Weller
Thomas Weller

Reputation: 59238

XSLX files are ZIP files internally. You can unpack XLSX files with 7Zip for example. Perhaps the file you want to open is not a XLSX file but a XLS file (old office file format) instead or something totally unrelated.

It seems you are looking for new files continuously with

glob.glob('btc*.xlsx')

It's possible that you found a new file but that file has not been fully written yet. This could be improved by waiting the 10 seconds after a new file was found (though, just waiting an arbitrary time will not resolve the issue 100%)

There is no stack trace. The only error is that. It says "file is not a zip file" and it repeats in an infinite loop because of the while loop.

There is no stack trace, because you catch the exception and only print its message. Use

traceback.print_exc()

to print the last exception, or, even better, don't catch the exception.

Upvotes: 1

Related Questions