0

If I have a python code that currently works with static files but I wish to change it in a way that would allow it to accept dynamic files as well, how can I do so?

Like this is my code in python:

row = run('cat '+'/Users/minks/Documents/X-test.txt'+" | wc -l").split()[0]
print("Test row size:")
print(row)
matrix_tmp_test = np.zeros((int(row),col), dtype=np.int64)
print("Test matrix size:")
print(matrix_tmp_test.size)

Now if that file X_test.txt is dynamic and needs to be provided by command line instead, how can I do so via command line such that it goes and fit into the statement? There are other file calls in the python script as well so how can I ensure the dynamic file goes and fits into the above statement only?

minks
  • 2,651
  • 4
  • 19
  • 28
  • You want to provide a file as input from the commandline? – Tim Feb 19 '16 at 12:17
  • Possible duplicate of [Python: user input and commandline arguments](http://stackoverflow.com/questions/70797/python-user-input-and-commandline-arguments) – SiHa Feb 19 '16 at 12:31

1 Answers1

3

You can pass file name from command line as

python_program.py filename


import sys
sys.argv[1] # would be your file name in program
row = run('cat '+ sys.argv[1]+" | wc -l").split()[0]
AlokThakur
  • 3,371
  • 1
  • 17
  • 31