0

head = csv.reader(open("header.csv"))

I have something like this. here is it possible to provide part of the input in the command line like this.

head = csv.reader(open("sys.argv[1]".csv")) 

So that I can launch the script as

python test.py header
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
Tim
  • 45
  • 8
  • 2
    Possible duplicate of [How do I access command line arguments in Python?](https://stackoverflow.com/questions/4033723/how-do-i-access-command-line-arguments-in-python) – vahdet Mar 18 '19 at 14:26

2 Answers2

1

Use string formatting

head = csv.reader(open('{}.csv'.format(sys.argv[1])))

or in python 3.6 or later

head = csv.reader(open(f'{sys.argv[1]}.csv'))
FHTMitchell
  • 11,793
  • 2
  • 35
  • 47
1

Since python 3.6 you can use f-strings. https://www.python.org/dev/peps/pep-0498/

head = csv.reader(open(f'{sys.argv[1]}.csv'))
Uli Sotschok
  • 1,206
  • 1
  • 9
  • 19