0

I am new to python programming, but basically I want to have a script where when given a file path from the command line (the file being a .csv file), the script will take this file and turn it into a list.

Code I have seen for this problem usually doesn't take the file from the command line, rather it just has the exact filename in the code. I want to use this code on many different files, so hard coding the file name into the program is not an option.

CodeLikeBeaker
  • 20,682
  • 14
  • 79
  • 108
user8233898
  • 43
  • 1
  • 8
  • This question should have everything you need: https://stackoverflow.com/questions/1009860/command-line-arguments-in-python. For a very simple script you can see the sys.argv related answer, then level up to argparse which will be useful in the long term. – kristaps Jun 29 '17 at 20:41

1 Answers1

2

The only change you need to make to the code that you've already found is this:

import sys
filepath = sys.argv[1]
... # process file

And now, you invoke your script like this: ./script.py /path/to/file.

sys.argv is used to read command line arguments. Optional commands start from index 1, because argv[0] stores the name of your script (script.py).

cs95
  • 379,657
  • 97
  • 704
  • 746