Search code examples
pythonpandasread-csv

OSError: [WinError 433] A device which does not exist was specified: 'csv_files'


I am trying to troubleshoot this problem which appears to be intermittent. the function is called once per hour.

  • sometimes the code runs for several days (hundreds of loops)
  • other times it runs for just a day (20 or so loops)
OSError: [WinError 433] A device which does not exist was specified: 'csv_files'

I trace the code back to this function, which sometimes returns an empty dataframe.

import pandas as pd

def get_existing_df(symbol:str)-> pd.DataFrame:
    '''
    check if file exists & get the the existing dataframe for a given symbol
    
    args:
        symbol (str): the ticker symbol

    return (df, last_trade):
        df: (DataFrame) of the last trades (empty df if no trades)    
        
    
    '''
    file_name = 'csv_files/all_trades_' + symbol + '.csv'

    if os.path.exists(file_name):
        df = pd.read_csv(file_name, index_col=0)
    else:
        df = pd.DataFrame()
    
    return df

The code uses a relative path. I can also confirm the obvious:

  1. the path exists
  2. the file exists
  3. i believe that i have permission (the full path is my local google drive).

But every now and again the method gives me an empty dataframe, which breaks the code (with the above error).

What is causing this and how can i prevent this ?


Solution

  • Given that the file and path (to file) exist, it is likely to be the connection.

    So, I propose the following additional check which uses the socket module to check the connection:

    import socket
    
    
    def is_internet_connected():
        try:
            socket.create_connection(("www.google.com", 80), timeout=3)
            return True
        except Exception:
            return False
    
    connected = is_internet_connected()
    print('checking connection:', connected)
    
    

    This checks the internet connection and if it is down for more than 3 seconds, then returns False.

    This means that the code can be avoided (if not connected:) in the event that the connection is down.