-1

I am trying to get a string of text and write it a file if the file does not exist. Here is the code so far.

#!/usr/bin/python

import os.path as path

configFileLocation = "~/.sajan.io-client"

def createConfig():
    print "Creating config file\n"
    apikey = input("Enter the API key: ")
    configFile = open(configFileLocation, "w")
    configFile.write(apikey)
    configFile.close()

if path.isfile(configFileLocation) == False:
    createConfig()

When run, I get the prompt to Enter the API key:, and whatever I enter in doesn't seem to be used as a string. Here is the output I get and I can't make sense of it. It's almost as if Python is trying to use what I enter in as a variable in the script.

Creating config file

Enter the API key: somerandomapikey123
Traceback (most recent call last):
  File "./new-thought.py", line 15, in <module>
    createConfig()
  File "./new-thought.py", line 9, in createConfig
    apikey = input("Enter the API key: ")
  File "<string>", line 1, in <module>
NameError: name 'somerandomapikey123' is not defined
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Sajan Parikh
  • 4,668
  • 3
  • 25
  • 28

1 Answers1

0

You are using Python2, use raw_input instead of input. raw_input will make sure the input is stored as a string:

Python 2.7.3 (default, Jan  2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> result = input('Hello: ')
Hello: world
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'world' is not defined
>>> result = raw_input('Hello: ')
Hello: world
>>> result
'world'
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284