Try to use import sys
and to read the arguments passed sys.argv
which is a list. The parameters you passed start from the second (index 1)
An example is the following.
import sys
if __name__ == "__main__":
print(type(sys.argv))
if len(sys.argv) <2:
print("Expecting file credentials")
exit(1)
f = open(sys.argv[1])
for line in f:
print(line[:len(line)-1]) #avoid printing new line char
Specify how your credential file is supposed to be to extract the fields.
EDIT: the input file is a JSON
import sys
import json
if __name__ == "__main__":
#print(type(sys.argv))
if len(sys.argv) <2:
print("Expecting file credentials")
exit(1)
f = open(sys.argv[1])
#for line in f:
# print(line[:len(line)-1]) #avoid printing new line char
###READ FROM JSON
data = f.read()
print("---------")
print(data)
content = json.loads(data)
print(content["postgres"]["host"])
print(content["postgres"]["password"])
Note one thing that it was giving me problems: you can read the file with f.read()
once. If you call it again there is not anything to read.