2015-04-02 76 views
0

我當前的代碼如下所示,但我無法弄清楚如何訪問一個文本文件(例如「john.txt」)如何從我的電腦訪問文本文件?

def read_script(): 
     while True: 
      try: 
       filename = input('Please Enter Text Name: ') 
       F = open (filename, 'r') 
       script - F.read() 
       F.close() 
       slist = script.split() 
       return slist 
      except OSError: 
       print ('Oops! That file does not exist! Try spelling it correctly: ') 

    def pig_english(): 
     letterlist = [i + i[0] for i in read_script()] 
     ayList = [i + 'ay' for i in letterlist] 
     delaylist = [i[1:] for i in aylist] 
     print (delaylist) 


    read_script() 
    pig_english() 
+1

檢測到的問題:'腳本 - F.read()' – Kevin 2015-04-02 19:38:59

+0

哎呀,但即使我把腳本= F.read()我得到一個錯誤提示。嗯 – CombuderSci 2015-04-02 19:42:34

+1

什麼樣的錯誤提示? – Kevin 2015-04-02 19:44:03

回答

0
你想要的raw_input

(),而不是輸入()。 input()將用戶輸入解釋爲對象而不是字符串。

+1

取決於版本。如果OP使用2.7,我同意你的看法。但在3.X中,'input'返回一個字符串。 – Kevin 2015-04-02 19:46:25

+0

是的,我正在使用3.X,所以我的輸入自動是一個字符串。 – CombuderSci 2015-04-02 19:49:28

+0

這個問題標記爲[[python-3.x]'... – MattDMo 2015-04-02 19:59:53

相關問題