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