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
  Reading and storing a line of output from pexpect child eagerissac 1 4,144 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  Receive Input on Same Line? johnywhy 8 606 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  problem in using input command akbarza 4 996 Oct-19-2023, 03:27 PM
Last Post: popejose
  capturing multiline output for number of parameters jss 3 763 Sep-01-2023, 05:42 PM
Last Post: jss
  output provide the filename along with the input file processed. arjunaram 1 902 Apr-13-2023, 08:15 PM
Last Post: menator01
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,033 Jan-16-2023, 07:38 PM
Last Post: Skaperen
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,026 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Command line argument issue space issue mg24 5 1,279 Oct-26-2022, 11:05 PM
Last Post: Yoriz
  accept command line argument mg24 5 1,236 Sep-27-2022, 05:58 PM
Last Post: snippsat
  Os command output in variable shows wrong value paulo79 2 1,467 Apr-09-2022, 03:48 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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