Problem when reading file with DatePicker

Hi all, I’m facing quite weird problem as below and I’m not sure whether it is problem of pandas, of Dash or drive format.

I’m using dmc.DatePicker then use it to select file to read in the folder by using glob.
When I put file in C drive (SSD format) it worked well, but when I transfer file from C drive to new drive such as G (HDD format) drive it raised error that said local variable '...' referenced before assignment.

My sample code as below:

import pandas as pd
import numpy as np
from datetime import datetime as dt
import plotly.express as px
from dash import Dash, html, dcc, Input, Output, State
import dash_table
import dash_bootstrap_components as dbc
import glob
import os
from pandas.tseries.offsets import BDay
import plotly.graph_objects as go
import dash_ag_grid as dag
import dash_mantine_components as dmc
from datetime import datetime, date
import base64
import io


app = Dash(__name__)

app.layout = dmc.MantineProvider([
    html.Div([
        dmc.Grid([
                dmc.Col([
                    dmc.DatePicker(id='datepickersingle_15',
                                   label='Date Selection',
                                   description='Select date of data',
                                   value=(dt.today() - BDay(2)).date(),
                                   style={'width': 150})
                ], span='auto'),
                dmc.Col([
                    dmc.DatePicker(id='datepickersingle_16',
                                   label='Date Selection',
                                   description='Select date of data',
                                   value=(dt.today() - BDay(1)).date(),
                                   style={'width': 150})
                ], span='auto'),
                dmc.Col([
                    dmc.NumberInput(id='input_13',
                                    label='Maximum',
                                    description='Maximum amount',
                                    value=3000000,
                                    precision=4,
                                    style={'width': 150})
                ], span='auto'),
                dmc.Col([
                    dmc.NumberInput(id='input_14',
                                    label='Minimum',
                                    description='Minimum amount',
                                    value=-3000000,
                                    precision=4,
                                    style={'width': 150})
                ], span='auto'),
                dmc.Col([
                    dmc.Button('Submit', color='gray', id='btn_8', n_clicks=1)
                ], span='auto'),
            ], align='center'),
            dmc.Grid([
                dmc.Col([
                    dmc.Title(f'수신 변동 현황', order=2)
                ], span='auto'),
                dmc.Col([
                    dmc.RadioGroup([dmc.Radio('고객별', value='cus'),
                                    dmc.Radio('상품별', value='pro')],
                                   id='cus_pro',
                                   value='cus',
                                   label="Select report type")
                ], span='auto'),
            ], align='center'),
            dmc.Title(f"Click on the download button below to export data", order=6),
            dmc.Button('Download', variant='outline', id='export_10'),
            dmc.Grid([
                dmc.Col([
                    dmc.LoadingOverlay([html.Div(id='table_container_13')], loaderProps={'variant':"dots"})
                ], span='auto')
            ], align='center')
        ])
])

@app.callback(Output('table_container_13', 'children'),
              Input('btn_8', 'n_clicks'),
              Input('cus_pro', 'value'),
              State('datepickersingle_15', 'value'),
              State('datepickersingle_16', 'value'),
              State('input_13', 'value'),
              State('input_14', 'value'), prevent_initial_call=True)
def update_table_4(n_clicks, cus_pro, date_15, date_16, input_13, input_14):
    ############################################################################################
    # Xóa đoạn này nếu như lấy theo dữ liệu tổng
    date_1 = '*' + date_15[:4] + date_15[5:7] + date_15[8:10] + '.txt'
    date_2 = '*' + date_16[:4] + date_16[5:7] + date_16[8:10] + '.txt'
    print(date_1)
    for file in glob.glob(r'C:\Users\admin\Báo cáo ngày' and date_1):
        dep1 = pd.read_table(file, sep='|',
                             index_col=None, header=0,
                             dtype={'BR_CD': str, 'ACCOUNT_NUMBER': str, 'BAS_DT': str, 'CUSTOMER_NUMBER': str})
    for file in glob.glob(r'C:\Users\admin\Báo cáo ngày' and date_2):
        dep2 = pd.read_table(file, sep='|',
                             index_col=None, header=0,
                             dtype={'BR_CD': str, 'ACCOUNT_NUMBER': str, 'BAS_DT': str, 'CUSTOMER_NUMBER': str})

    depo_total = pd.concat([dep1, dep2], axis=0, ignore_index=True)



if __name__ == '__main__':
    app.run_server(debug=False)

Note that my C drive almost full so I decided to transfer file folder to another drive. If I kept files in old folder, Dash still worked well.

Thank you

I’m facing a problem with my Dash application when reading files from a different drive. The application works fine when files are on the C drive (SSD), but moving them to the G drive (HDD) results in an error: “local variable ‘…’ referenced before assignment.” This issue occurs when using dmc.DatePicker to select dates and then reading files with glob.

Here’s a simplified version of my code:

python

Copy code

import pandas as pd
import glob
from dash import Dash, html, Input, Output, State
import dash_mantine_components as dmc
from datetime import datetime as dt
from pandas.tseries.offsets import BDay

app = Dash(__name__)

app.layout = dmc.MantineProvider([
    html.Div([
        dmc.Grid([
            dmc.Col([
                dmc.DatePicker(id='datepickersingle_15',
                               label='Date Selection',
                               value=(dt.today() - BDay(2)).date(),
                               style={'width': 150})
            ], span='auto'),
            dmc.Col([
                dmc.Button('Submit', id='btn_8', n_clicks=1)
            ], span='auto')
        ], align='center')
    ])
])

@app.callback(Output('table_container_13', 'children'),
              Input('btn_8', 'n_clicks'),
              State('datepickersingle_15', 'value'),
              prevent_initial_call=True)
def update_table_4(n_clicks, date_15):
    date_1 = '*' + date_15[:4] + date_15[5:7] + date_15[8:10] + '.txt'
    for file in glob.glob(r'G:\path\to\files\ ) and date_1):
        dep1 = pd.read_table(file, sep='|', index_col=None, header=0)
    # Further processing...

if __name__ == '__main__':
    app.run_server(debug=False)

Your Texas Benefits

  • Error occurs only when files are on the G drive.
  • No issues when files are on the C drive.
  • Might be related to drive format or file access permissions.

I need help identifying the root cause and resolving this error. Thank