Check File Existence and Process CSV or XLS Files using Pandas

Check File Existence and Process CSV or XLS Files using Pandas

Reading CSV File r Excel file  as a Parameter

 

To read a CSV (Comma Separated Values) or Excel file as an input parameter in a programming language like Python,follow the steps mentioned below:

 

 Import the necessary libraries: Import the required libraries, such as csv or pandas, to read and manipulate the CSV file or read_xls

 

Process the CSV /Excel data: Depending on your requirements, you can process the data in the CSV file. For example, you can iterate over the rows and access values from specific columns.

 

Step 1: Import library

 

 import os

import pandas as pd

import sys

 

step2 : Read csv file

 

def process_csv(file_path):

    # Read CSV using pandas

    df = pd.read_csv(file_path)

    

    # Process the DataFrame as needed

    # For example, you can print the first few rows

    print(df)

 

step 3: Read Excel file

 

def process_excel(file_path):

    # Read Excel using pandas

    df = pd.read_excel(file_path,sheet_name='data')

    

    # Process the DataFrame as needed

    # For example, you can print the first few rows

    print(df)

 

 

 

step 5: Below function will check the csv or excel is exist or not, if file exist then it will process the file else throw appropriate error message.

It will check file type also.

 

def read_or_error_file(folder_path,file_name):

        try:

        if os.path.exists(os.path.join(folder_path, file_name)): 

            filename=os.path.join(folder_path,file_name)

            if filename.endswith('.csv'):

                process_csv(filename)

            elif filename.endswith('.xls') or filename.endswith('.xlsx'):

                process_excel(filename)

            else:

                print("Unsupported file type")

        else:

            raise FileNotFoundError("The specified file does not exist in the folder.")

 

    except FileNotFoundError:

        print("Folder or file not found")

    except Exception as err:

        print("Undefined error",err)

 

if __name__=='__main__':

    if len(sys.argv)!=3:

        print("Usage: python program_name.py file")

    else:

        input_folder=sys.argv[1]

        input_file=sys.argv[2]

        read_or_error_file(input_folder,input_file)

 

# folder_path = "g:/ETL_Automation/data/"

# file_name = "datasingle.xlsx"

# read_or_error_file(folder_path,file_name)

 


Post a Comment

0 Comments