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
  Accessing varying command line arguements Rakshan 3 166 Jul-28-2021, 03:18 PM
Last Post: snippsat
  Reading and storing a line of output from pexpect child eagerissac 0 183 Jun-17-2021, 12:22 PM
Last Post: eagerissac
  How to make input come after input if certain line inserted and if not runs OtherCode Adrian_L 6 791 Apr-04-2021, 06:10 PM
Last Post: Adrian_L
  Multi-line console input lizze 4 608 Dec-26-2020, 08:10 AM
Last Post: lizze
  Code giving same output no matter the input. Yort 2 549 Dec-20-2020, 05:59 AM
Last Post: buran
  Handling multi-input/output audio in python bor1904 4 937 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  calling a function and argument in an input phillup7 3 710 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  Passing List of Objects in Command Line Python usman 7 998 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  single input infinite output problem Chase91 2 578 Sep-23-2020, 10:01 PM
Last Post: Chase91
  bad input line 3 how t fix it youssef210 2 684 Aug-27-2020, 04:57 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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