-1

.text ファイルを作成して単語を入力し、プログラムでファイルを開く方法を知りたいと思っていました。.text ファイルの作成方法を知りたいだけです。

コードを実行しようとすると、.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
4

2 に答える 2

1

ファイルが存在しない場合は、書き込みモードでファイルを開くと作成されます。

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

上記のコードでmyfileは、ファイル オブジェクトになります。

ここに と のリファレンスがopenありwithます。

于 2013-11-05T05:01:55.927 に答える
1
with open('myfile.txt', 'w') as f:
  f.write('potato')
于 2013-11-05T05:02:19.697 に答える