ファイルへの追加
ファイルを扱う際にサポートされているいくつかのモードを思い出しましょう:
モード | 操作 | 例 |
'r' | 読み込み(デフォルト) | with open('document.txt', 'r') as f: |
'w' | 書き込み | with open('document.txt', 'w') as f: |
'a' | 追加 | with open('document.txt', 'a') as f: |
'r+' | 読み書き | with open('document.txt', 'r+') as f: |
'x' | 新規作成(存在する場合はエラー) | with open('document.txt', 'x') as f: |
コンピューターにはさまざまなファイルが存在します。あなたはそれらにいくつかの行を追加するよう求められています。
入力は2行からなります。最初の行はファイル名です。2番目の行はそのファイルに追加したい内容です。
プログラムは、まず指定された行をファイルに追加し、その後ファイルの全内容を出力する必要があります。
入力 | doc.txt(初期内容) | 出力 | doc.txt(結果の内容) |
doc.txt
This is the second line | This is the first line | This is the first line
This is the second line | This is the first line
This is the second line |
入力 | empty.txt(初期内容) | 出力 | empty.txt(結果の内容) |
empty.txt
Adding some content | ㅤ | Adding some content | Adding some content |
入力 | multiline.txt(初期内容) | 出力 | multiline.txt(結果の内容) |
multiline.txt
The final line! | This is
some very
very long
document
with
many
lines of text… | This is
some very
very long
document
with
many
lines of text…
The final line! | This is
some very
very long
document
with
many
lines of text…
The final line! |
Constraints
Time limit: 1 seconds
Memory limit: 512 MB
Output limit: 1 MB