File Descriptor error when opening in "wb" mode

26 Views Asked by At

I am reading in some data from a csv. Storing them in a dict. Creating two SymbolInfo objects and also storing these in a dict. I then store each of these dicts in a tuple, and attempt to dump this tuple using pickle.

symbols = ["EURJPY", "EURGBP"]
tup_0 = {}
tup_1 = {}
for symbol in symbols:
    sym_info_obj = symbol_info(symbol)
    sym_mtsim_obj = SymbolInfo(sym_info_obj)
    tup_0[symbol] = sym_mtsim_obj
    main_direc = os.path.abspath(os.getcwd()) + "\\" + "Code and Data"
    symbol_file = f"1M-{symbol}-1S.csv"
    full_file = main_direc + "\\" + symbol_file
    data = pd.read_csv(full_file)
    data["Time"] = data["Date"].astype(str) + " " + data["Timestamp"]
    data = data.drop(["Timestamp", "Date"], axis=1)
    tup_1[symbol] = data

tup = (tup_0, tup_1)

full_path = os.path.join(os.getcwd(), "Code and Data", "test.pkl")

with open(full_path, "wb") as fh:
    pickle.dump(tup, fh) 

This is the code i am using, where symbol_info() is a function which returns a SymbolInfo object.

However, when i try to create a file handle to pass into the pickle.dump method, i either:

  1. Get a file or directory not found error.
  2. Have created the file manually and then get a Bad file descriptor error
  3. Try to use regular "w" mode - which opens the file, but then get a type error saying i must write a string not bytes.

Both the .py file, the .csv and .pkl file (when i manually created it) are all under the same folder

0

There are 0 best solutions below