Python Forum

Full Version: How to input & output parameters from command line argument
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Want to give two parameters to this python code. Where is my input files and where to save my output file. This code is taking any excel file and convert them into different csv files. How exactly can I code these inside my script? I am running this code from terminal
python exceltocsv.py 'C:\Users\Sg\Desktop\Test-1\Input' 'C:\Users\Sg\Desktop\Test-1\Output'
Can anyone help me to tweak this code.

This is my code:
from pathlib import Path
from itertools import chain
import time
import parser
import argparse
import pandas as pd
import os
import warnings
import re

warnings.filterwarnings("ignore")

parser = argparse.ArgumentParser(description="Process some integers.")

parser.add_argument("path", help="define the directory to folder/file")
parser.add_argument("--verbose", help="display processing information")

start = time.time()


def main(path_xlsx, verbose):
    if re.search(r"\.xls[xm]?$", str(path_xlsx).lower()) and path_xlsx.is_file():
       xlsx_files = [Path(path_xlsx)]
    else:
       xlsx_files = list(chain(Path(path_xlsx).glob("*.xls"), Path(path_xlsx).glob("*.xlsx"), Path(path_xlsx).glob("*.xlsm")))

    df = pd.DataFrame()
    for fn in xlsx_files:
        all_dfs = pd.read_excel(fn, sheet_name=None)
        for sheet_name, df in all_dfs.items():
            df = df.assign(DataSource=Path(fn.name))
            x=os.path.splitext(fn.name)[0]
            df.to_csv(os.path.join(path,f'{x}_{sheet_name}.csv'),index=False)            
        
if __name__ == "__main__":
    start = time.time()
    args = parser.parse_args()
    path = Path(args.path)
    verbose = args.verbose
    main(path, verbose)  #Calling Main Function
    print("Processed time:", time.time() - start)  #Total Time
for simple input, you can use sys.argv
example:

sysargv.py
import sys

for n, item in enumerate(sys.argv):
    print(sys.argv[n]) 
running python sysargv.py one beta delta hello
Output:
sysargv.py one beta delta hello
Note: sys.argv[0] is the script name.

A better way is to use argparse: https://docs.python.org/3/howto/argparse.html