1

I am trying to run the iris_dataset using os.system from python, then while taking the values I am copying the value from iris_dataset to Temp and then I am opening the Temp file and using it like below.

import os

import sys

os.system("/home/mine/Desktop/C4.5/c4.5 -u -f iris_dataset")

os.system("/home/mine/Desktop/C4.5/c4.5rules -u -f iris_dataset > Temp")

f=open('Temp')

Once I am done with my program I am executing my program like : python3 prog_name.py In this case whenever I am using any other dataset apart from iris_dataset, I need to open the program again and change that name in the above code where iris_dataset is written.

Instead of this I just want to make a change i.e while executing the program I want to write : python3 prog_name.py my_data_set_name in kind of command line, so that it becomes more easy to change the datasets as per my wish.

Dev
  • 576
  • 3
  • 14
  • https://stackoverflow.com/questions/4033723/how-do-i-access-command-line-arguments – Mahrkeenerh Oct 17 '21 at 10:55
  • Does this answer your question? [How do I access command line arguments?](https://stackoverflow.com/questions/4033723/how-do-i-access-command-line-arguments) – DDomen Oct 17 '21 at 11:05

2 Answers2

1

You could use click to create a nice console line interface. It can give you nice help text, options etc.

For example:

import os
import click

@click.command()
@click.argument("dataset")
def init(dataset):
    """
    DATASET - Dataset to process
    """
    process_dataset(dataset)


def process_dataset(dataset):
    os.system(f"/home/mine/Desktop/C4.5/c4.5 -u -f {dataset}")
    os.system(f"/home/mine/Desktop/C4.5/c4.5rules -u -f {dataset} > Temp")
    f=open('Temp')
    ...

if __name__ == "__main__":
    init()
vladsiv
  • 2,718
  • 1
  • 11
  • 21
0

Use sys.argv

import os

import sys

dataset = sys.argv[1]

os.system(f"/home/mine/Desktop/C4.5/c4.5 -u -f {dataset}")

os.system(f"/home/mine/Desktop/C4.5/c4.5rules -u -f {dataset} > Temp")

f=open('Temp')

Please note that I used python's f-strings for better code readability. Feel free to use the dataset variable in any way you see fit

sys.argv documentation

Nik Vinter
  • 145
  • 10
  • Yeah! Got it. Thank you. It is working fine. – Dev Oct 17 '21 at 11:01
  • Your answer (even if correct) was alredy answered in another [question](https://stackoverflow.com/questions/4033723/how-do-i-access-command-line-arguments?noredirect=1&lq=1). You should check similar questions (usually also present on the right panel) before replying – DDomen Oct 17 '21 at 11:06
  • Okay, I'll do that from next time. – Dev Oct 17 '21 at 11:31