0

I want to use arguments which will then be translated into variable that will hold a value:

I get some values from a CSV file then upload it to Googles' Sheet Docs.

Here is a snippet of my code [mycode.py]:

f101 = open('/home/some_directory/101_Hrs.csv')
f102 = open('/home/some_directory/102_Hrs.csv')
f103 = open('/home/some_directory/103_Hrs.csv')
f112 = open('/home/some_directory/112_Hrs.csv')

csv_f101 = csv.reader(f101)
csv_f102 = csv.reader(f102)
csv_f103 = csv.reader(f103)
csv_f112 = csv.reader(f112)

I want to use an argument (101,102,103 or 112) from the terminal (for example mycode.py 101), where I can use the [101] to be concatenated with the f to getf101 and also open('/home/some_directory/[101]_Hrs.csv') where [101] is a number that can be replaced by the argument from the terminal.

How is this done?

3
  • 5
    Instead of dynamically creating variables, you want to use a dict. Also you may want to look into for loops ;). Commented Nov 9, 2015 at 1:04
  • You don't need separate variables. Use a for loop to open a file and upload it, then do the next file, etc. To substitute something in the file name you use string formatting: print 'abc-{0}'.format(101) docs.python.org/2/library/string.html Commented Nov 9, 2015 at 1:28
  • maybe os.walk() with a loop make you happy. Commented Nov 9, 2015 at 3:46

2 Answers 2

1

A couple of easy tutorials for using input arguments in python programs:

The basic idea is to use the inbuilt sys module, which lets you access the inputs via argv.

import sys

nInputs = len(sys.argv)

print 'Number of arguments = ', nInputs
print 'Inputs = ', str(sys.argv)

if (nInputs >= 2):
    strFilename = '/home/some_directory/' + sys.argv[1] + '_Hrs.csv'
    print 'Filename = ', strFilename

Then when you run:

>> python mycode.py 101
Number of arguments = 2
Inputs = ['mycode.py', '101']
Filename = /home/some_directory/101_Hrs.csv
Sign up to request clarification or add additional context in comments.

2 Comments

Should there be a : after the if (nInputs >= 2) ?
Also - strInputs[1] indexes 1st character from ['mycode.py', '101'] which is [ and strInputs[4] = m ...`
0

Check out argparse, which has more features than sys.argv. You don't necessarily need all of them with your current requirements, but it is more flexible and lets you avoid some reinvention:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('arguments', nargs='+')
args = parser.parse_args()
print('number of supplied arguments:{}'.format(len(args.arguments)))
for argument in args.arguments:
    print('supplied argument:{}'.format(argument))

It also has an excellent tutorial written by a frequent Stack Overflow contributor: https://docs.python.org/2/howto/argparse.html

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.