Search code examples
pythonpickle

TypeError: 'str' object cannot be interpreted as an integer when using pickle


When I try and open the pickle file, this error appears:

Exception in thread Thread-1 (coinsv_thread):
Exception in thread Thread-2 (coinsv_thread):
Traceback (most recent call last):
Traceback (most recent call last):
  File "C:\Users\Gregory\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner
  File "C:\Users\Gregory\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner
    self.run()
  File "C:\Users\Gregory\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run
    self.run()
  File "C:\Users\Gregory\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File "c:\Users\Gregory\Desktop\Y7 Homework\Actual cOMPUTING ASESSMENT\Assesment-Y7\functionality.py", line 30, in coinsv_thread
    self._target(*self._args, **self._kwargs)
  File "c:\Users\Gregory\Desktop\Y7 Homework\Actual cOMPUTING ASESSMENT\Assesment-Y7\functionality.py", line 30, in coinsv_thread
    with open('rlog.pkl', 'rb') as handle:
         ^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'str' object cannot be interpreted as an integer
    with open('rlog.pkl', 'rb') as handle:
         ^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'str' object cannot be interpreted as an integer

Here is the code:


def coinsv_thread(g):
    #try:
    if True:
        import pickle
        import webclient
        SERVER_URL = "http://gregglesthegreat.pythonanywhere.com/"
        with open('rlog.pkl', 'rb') as handle:
            a = pickle.load(handle)
            handle.close()
        if a[0] == 1:
            my_dict = webclient.get_variable(SERVER_URL, "d_pgp_LOGIN")
            my_user = my_dict.get(a[1])
            my_coins = my_user[1]
            my_new_coins = int(my_coins) + int(g)
            my_user[1] = my_new_coins
            my_dict[a[1]] = my_user
            webclient.update_variable(SERVER_URL, "d_pgp_LOGIN", my_dict)
        else:
            print("Not logged in.")
    #except Exception as e:
    #    print("Fail: ", e)

def coinsv(g):
    thread = threading.Thread(target=coinsv_thread, args=(g,))
    thread.start()

I have tried to open the file in the python shell, which works, and I have asked AI online for help.


Solution

  • probably because of very bad practice of importing all objects of os with from which ends up shadowing built-in open by os.open which expects the second argument as integer:

    >>> from os import *
    >>> open('flllf','ff')
    Traceback (most recent call last):
      File "<interactive input>", line 1, in <module>
    TypeError: an integer is required (got type str)
    

    An alternate quick solution if from os import * must remain is to add this after importing:

    from os import *
    del open
    

    that deletes the reference of open as os.open and the built-in open is visible again.

    That said, this makes the code hard to read. The recommended solution is:

    • remove from os import *
    • prefix all calls to os modules by os. (the calls will fail if not prefixed, test the script thoroughly)