2014-10-20 6 views
-1

파일이없는 경우 문자열을 가져 와서 파일에 기록하려고합니다. 지금까지 코드가 있습니다. 실행하면문자열로 키보드 입력 저장

#!/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() 

, 나는 Enter the API key:에 프롬프트를, 나는에 입력 어떤 문자열로 사용하지 않는 것. 다음은 내가 얻은 결과이며, 나는 그것을 이해할 수 없다. 마치 파이썬이 스크립트에서 변수로 입력 한 것을 사용하려고하는 것처럼 보입니다.

당신은 Python2를 사용하는

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 
+0

whats your python version? 터미널에서'python --version'을하십시오! – Kasramvd

답변

0

설정 파일을 작성, raw_input 대신 input 사용합니다. raw_input은 입력이 문자열로 저장되는지 확인합니다.

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'