Python Forum
How to input & output parameters from command line argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to input & output parameters from command line argument
#1
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
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Βad Input on line 12 Azdaghost 5 1,276 Apr-19-2025, 10:22 PM
Last Post: Azdaghost
  interactive process for input and output maiya 1 596 Mar-27-2025, 08:40 AM
Last Post: Gribouillis
  Insert command line in script lif 4 1,011 Mar-24-2025, 10:30 PM
Last Post: lif
  How to revert back to a previous line from user input Sharkenn64u 2 964 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  How to run shell command, capture the output, then write it into textfile? tatahuft 4 978 Dec-20-2024, 02:13 PM
Last Post: Axel_Erfurt
Question [SOLVED] Same input different output antarling 2 929 Oct-25-2024, 11:28 PM
Last Post: antarling
  Simplest way to run external command line app with parameters? Winfried 2 1,273 Aug-19-2024, 03:11 PM
Last Post: snippsat
  Reading and storing a line of output from pexpect child eagerissac 1 6,645 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  Receive Input on Same Line? johnywhy 8 2,651 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  problem in using input command akbarza 4 3,338 Oct-19-2023, 03:27 PM
Last Post: popejose

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020