0

I want to run my program if I type a specific command. My Python program should take the test file as a command line argument. For example in the command prompt or the terminal, my program should run if you type “python my_program.py test.txt”.

Turn
  • 6,656
  • 32
  • 41
  • Given the diversity of answers you've received so far, it is probably obvious that it isn't clear at all what you are asking. What do you want to type and what do you want to happen when you type it? – Turn Feb 07 '18 at 00:27
  • 1
    Perhaps you should explain why you can't just type `python my_program.py test.txt`. – John Gordon Feb 07 '18 at 00:36

4 Answers4

1

use the execfile built-in function from python, you can google it, there are many answers as well on this site with this info. Make research before asking.

ChameleonX
  • 87
  • 1
  • 10
1

Very Simple way is to create Alias on Linux (If you are comfortable and applicable):

alias my_command='python my_program.py' //Provide full path of the python script

Running the command

 my_command test.txt

(If linux)

BetaDev
  • 4,516
  • 3
  • 21
  • 47
1

How do I access command line arguments in Python?

This will tell you how to pass the argument into your program then you can access the file with open module

https://docs.python.org/2/library/functions.html#open

Vilyanare
  • 44
  • 2
1

Open command prompt, make sure you are in the right directory. If you are on windows you can simply hold shift and right click the folder and open command line here. If not you can just manually navigate to the directory by using the cd (change directory) command.

Type python my_program.py test.txt and it will work.

bhazero025
  • 337
  • 4
  • 15