Python code that is running in Jupyter Lab IDE is not working in command line

Following is my code

import pandas as pd
import os

def list_files(folder_path):
    files = []
    for file_name in os.listdir(folder_path):
        file_path = os.path.join(folder_path, file_name)
        if os.path.isfile(file_path):
    return files

file_path = input("Enter the output files path")
folder_path = input("Enter the input files path")
file_list = pd.Series(list_files(folder_path))
csv_data_source = []

for files in file_list:
    dfinv = pd.read_excel(folder_path + files,sheet_name="Import")
    dfscn = pd.read_excel(folder_path + files,sheet_name="ScannedPartNumbers")
    partmaster = pd.read_csv('part_master.csv',encoding='latin1',low_memory=False)
    dfscn = dfscn[dfscn['ScannedPartNumber'].str.len() != 71]
    dffnl = pd.DataFrame(columns = ['InvNo','PartNo','ScnSumQty','ScnAvgMRP','InvAvgMRP','InvGST','MRPDiff','TotDiff'])
    dfscn['scnpartno'] = dfscn['ScannedPartNumber'].str.slice(start=30,stop=48)
    dfscn['scnpartqty'] = dfscn['ScannedPartNumber'].str.slice(start=49,stop=55)
    dfscn['scnpartmrp'] = dfscn['ScannedPartNumber'].str.slice(start=56,stop=66)
    dfscn['scnpartno'] = dfscn['scnpartno'].str.strip()
    #dfscn['scnpartno'] = pd.to_char(dfscn['scnpartno'])
    dfscn['scnpartqty'] = pd.to_numeric(dfscn['scnpartqty'])
    dfscn['scnpartmrp'] = pd.to_numeric(dfscn['scnpartmrp'])
    dfinv['Qty'] = pd.to_numeric(dfinv['Qty'])
    dfinv['MRP'] = pd.to_numeric(dfinv['MRP'])
    unpano = pd.Series(dfscn['scnpartno'].unique())
    for pnum in unpano:
        if (dfscn.loc[dfscn['scnpartno'] == pnum,'scnpartmrp'].mean()) != (dfinv.loc[dfinv['Part #'] == pnum,'MRP'].mean()):
            dffnl.loc[pnum,'InvNo'] = files
            dffnl.loc[pnum,'PartNo'] = pnum
            dffnl.loc[pnum,'ScnSumQty'] = dfscn.loc[dfscn['scnpartno'] == pnum,'scnpartqty'].sum()
            dffnl.loc[pnum,'ScnAvgMRP'] = dfscn.loc[dfscn['scnpartno'] == pnum,'scnpartmrp'].mean()
            dffnl.loc[pnum,'InvAvgMRP'] = dfinv.loc[dfinv['Part #'] == pnum,'MRP'].mean()
            dffnl.loc[pnum,'InvGST'] = partmaster.loc[partmaster['Part Number'] == pnum,'GST'].mean()
            dffnl['MRPDiff'] = dffnl['ScnAvgMRP'] - dffnl['InvAvgMRP']
            dffnl['TotDiff'] = dffnl['MRPDiff'] * dffnl['ScnSumQty']
    dffnl.to_csv(file_path + files + ".csv",index = False)

print("Files generation successful")

for f1 in os.listdir(file_path):
    if f1.endswith('.csv'):
        f1 = os.path.join(file_path, f1)
        combineddf = pd.read_csv(f1)

merged_df = pd.concat(csv_data_source, axis = 0)
merged_df.to_csv(file_path + 'merged.csv',index=False)

print("Data merging successful")

The code is properly running in Jupyter Lab. But it’s not running in command line. Not even giving any error. New for coding. Somebody please help.

Tried to write a python code in Jupyter lab and getting correct output. But the same is not running from command line.

>Solution :

Open anaconda promt from start menu, navigate to project folder and run the python file which has the above code using below command. Replace myfile with your file name.


Leave a Reply