Text files · テキストファイル
Why files?
- Variables vanish when a program ends. A file keeps data on disk.
- Files let you save results and read them back later.
- They also hold more data than you would type by hand.
ファイルとは何のために?
- プログラムが終了すると変数は消滅します。ファイル はディスク上にデータを保持します。
- ファイルにより、結果を保存して後から読み戻すことができます。
- また、手入力で入力するよりも多くのデータを保持できます。
Open, use, close
open(name, mode)opens a file. The mode says what you will do."r"reads,"w"writes a fresh file (replacing any old text),"a"adds to the end.- Every file you open must be closed when you finish.
開く、使う、閉じる
open(name, mode)がファイルを開きます。モード は行う操作を示します。"r"は読み取り、"w"は新しいファイルに書き込み(既存のテキストを置き換え)、"a"は末尾に追加します。- 開いたファイルは処理が終了したら必ず閉じなければなりません。
The with block
with open(...) as f:opens the file and closes it for you.- All the work goes in the indented block.
- This is the safest way — the file is never left open.
with ブロック
with open(...) as f:がファイルを開き、自動的に閉じてくれます。- すべての作業はインデントされたブロック内に行われます。
- これは最も安全な方法であり、ファイルが開いたままになることはありません。
with open("greeting.txt", "w") as f:
f.write("Hi there\n")
with open("greeting.txt", "r") as f:
print(f.read())
Reading the lines
f.read()returns the whole file as one string.for line in f:reads it one line at a time.- Each line keeps its newline, so use
line.strip()to remove it.
ラインの読み取り
f.read()はファイル全体を一つの文字列として返します。for line in f:は1行ずつ読み取ります。- 各ラインには改行記号が含まれるため、除去するには
line.strip()を使用します。
with open("fruit.txt", "w") as f:
f.write("apple\npear\n")
with open("fruit.txt", "r") as f:
for line in f:
print(line.strip())
In Cambridge pseudocode
- The exam uses
OPENFILE,WRITEFILE,READFILE, andCLOSEFILE.
Cambridge擬似コードにおける表現
- 試験では
OPENFILE,WRITEFILE,READFILE,CLOSEFILEが使われます。
OPENFILE "notes.txt" FOR WRITE
WRITEFILE "notes.txt", "Hello"
CLOSEFILE "notes.txt"
OPENFILE "notes.txt" FOR READ
READFILE "notes.txt", line
CLOSEFILE "notes.txt"
OUTPUT line
Common mistakes
- Use
with open(...) as f:so the file closes itself. "w"erases the file first;"a"adds to the end.
よくあるミス
- ファイルを自動で閉じるために
with open(...) as f:を使用します。 "w"はまずファイルを削除し、"a"は末尾に追加します。
Now you try
- This playground keeps files between runs, so write the file first, then read it in the same task.
- Press Check answer to test your code.
あなたも試してみよう
- このプレイグラウンドでは実行間でファイルが保持されるため、先にファイルを書き込んでから同じタスク内で読み取ってください。
- 回答を確認 を押してコードを試してください。
Reading a file · ファイルの読み込み
A file is used in three steps: open, read, close. · ファイル操作には3つのステップがある:開く、読む、閉じる。
In one run: write the two lines Hello and World to a file called notes.txt, then open it again, read it, and print what you read. · 1回の実行で、2行の文字列 Hello と World を notes.txt という名前のファイルに書き込み、その後再度開いて読み取り、print を出力してください。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Write three lines to a file, then read it back and count the lines with a loop. Store the count in count (the answer is 3). · 3行をファイルに書き込み、ループで読み返して行数を数える。その数を count に格納する(答えは3)。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Write the numbers 10, 20, 30 to a file, one per line. Then read them back, convert each line with int(...), and add them up in total (the answer is 60). · 数字 10, 20, 30 をファイルに1行ずつ書き込みます。次にそれらを再読取し、各行を int(...) で変換して total で合計します(答えは 60 です)。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。