1

Been starring at this too long and I think I missed something dumb.. Writing a script to write information to a file. All the variables are strings being passed in from another function to write a file.

Here's my code:

 53 def makeMainTF():
 54         NameTag,mcGroupTag,mcIPTag = makeNameMCTag()
 55         yourName = getAccessSecretName()
 56         instanceType ="t2.micro"
 57         with open ("vlslabMain.tf","w") as text_file:
 58                 text_file.writelines(['provider \"aws\" {\n',
 59                                       '  ',
 60                                         'access_key = \"${var.access_key}\"\n',
 61                                       '  ',
 62                                         'secret_key = \"${var.secret_key}\"\n',
 63                                       '  ',
 64                                         'region     = \"${var.access_key}\"\n',
 65                                       '}\n\n\n',
 66                                       'resource \"aws_instance\" \"example\" {\n',
 67                                         '  ',
 68                                       'ami = \"${lookup(var.amis, var.region)}\"\n',
 69                                         '  ',
 70                                         'instance_type = \"%s\" \n}' % instanceType,
 71                                         '\n\n\n\n',
 72                                         'tags {\n',
 73                                         '   ',
 74                                         'Name = \"%s\"\n' % NameTag,
 75                                         '   ',
 76                                         'Multicast = \"%s,%s\"' % (mcGroupTag,mcIPTag),
 77                                         '   ',
 78                                         'Owner = \"%s\"' % yourName,
 79                                         '\n}'])

Not sure why I'm getting this error :

Enter Access Key: asd
Enter Secret Key: asd
Enter your name: asd
Access Key: asd
Secret Key: asd
Your full name is: asd
Traceback (most recent call last):
  File "terraTFgen.py", line 86, in <module>
    makeMainTF()
  File "terraTFgen.py", line 78, in makeMainTF
    'Owner = \"%s\"' % yourName,
TypeError: not all arguments converted during string formatting

Maybe I've been starring at it too long but I dont see the syntax mistake.

It actually wrote out

Enter Access Key: asd
Enter Secret Key: asd
Enter your name: asd
Access Key: asd
Secret Key: asd
Your full name is: asd

But the error is causing the script not to write to the actual file.

Thanks for the help! ****edit***

This is the function I used to get the yourName variable

 3 def getAccessSecretName():
  4         access_key = raw_input("Enter Access Key: ")
  5         secret_key = raw_input("Enter Secret Key: ")
  6         yourName = raw_input("Enter your name: ")
  7         print "Access Key: %s" % access_key
  8         print "Secret Key: %s" % secret_key
  9         print "Your full name is: %s" % yourName
 10         return access_key, secret_key, yourName
2
  • What's in yourName, seems like its a tuple. Commented Oct 8, 2016 at 3:16
  • I updated the edit with the function that calls the yourName. though I just put in what I thought was a string "ralph test" but strangely enough what writes to the file is :... Owner = "1234 4321 ralph test" which turned my string, into a tuple. Did I do something wrong in my getAccessSecretName()? Commented Oct 8, 2016 at 6:07

1 Answer 1

1

Replace line 78 with the following and try -

'Owner = \"%s\"' % " ".join(yourName),

Effectively, yourname seems to be a tuple.
The above code will convert it to a string value.

EDIT :- (Answer to the OP's last comment)

Look at line 10 of getAccessSecretName() function -

return access_key, secret_key, yourName

It returns a tuple of access_key, secret_key, and yourName.

So if you want only yourName to be written to your file,

(Option 1)

Replace line 55 in function getAccessSecretName() with

access_key, secret_key, yourName = getAccessSecretName()

This way you the three values are unpacked to different variables,

and replace line 78 with the following -

'Owner = \"%s\"' % yourName

EDIT 2 (Option II)

If you are only interested in yourName variable you can also do something like

yourName = getAccessSecretName()[2]

Replace line 55 with the above line. Here you will be copying the value of the tuple at a specific position to the variable yourName and ignoring other values.

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

3 Comments

It did get rid of the error so , yourName is just a raw input. I ran the program again and put int Ralph test. in my STDOUT it returned Enter Access Key: 1234 Enter Secret Key: 4321 Enter your name: ralph test But when I go look at what's written to the file I get : ... Owner = "1234 4321 ralph test" Could it be my function where I use to get the raw data is incorrect ? (Added function to main question) thanks a ton btw
Just did =D, any idea why its taking my 1st 2 inputs and grouping it into my output?
Ah thanks man that was it. It was very confusing because in line 54 I did just that because i needed all 3 variables. I didnt know that if I only put it 1 variable like I did in 55 then the 3 variables get grouped together. Fantastic!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.