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,859
  • 4
  • 21
  • 29

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,599
  • 1
  • 19
  • 32