Tim
Tim

Reputation: 45

how to input only part of filename in the command line in python

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

Upvotes: 0

Views: 42

Answers (2)

Uli Sotschok
Uli Sotschok

Reputation: 1236

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

Upvotes: 1

FHTMitchell
FHTMitchell

Reputation: 12157

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

Upvotes: 1

Related Questions