2013-11-05 42 views
-1

我想知道如何創建一個.text文件,這樣我就可以在其中輸入文字,然後在我的程序中打開文件。我只需要知道如何製作一個.text文件!如何在終端中創建一個.text文件python3.33

任何人都知道爲什麼我的代碼在我嘗試運行時無法打開我的.txt文件?

def readWords(filename): 
    words = [] 
    wordFile = open(words.txt, "r") 
    for line in wordFile: 
     line = line.upper() 
     words.extend(string.split(line)) 
    wordFile.close() 
    return words 
+0

不要在您的舊帖子中編輯新的/不同的問題,因爲它會破壞問題現有答案的上下文。你應該創建一個新的問題。 – wim

回答

1

打開以寫模式一個文件,如果它不將創建它爲你而存在:

with open("/path/to/file.txt", "w") as myfile: 
    # Do whatever 

在上面的代碼,myfile將文件對象。

這裏是openwith上的一個參考。

1
with open('myfile.txt', 'w') as f: 
    f.write('potato') 
相關問題