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.

1
  • This question should have everything you need: stackoverflow.com/questions/1009860/…. 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. Commented Jun 29, 2017 at 20:41

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.