1.
次のコードの実行結果を選んでください。
# sample.txt
# ---
# Python Programming
# File Handling
# ---
with open('sample.txt', 'r') as f:
print(f.read(7))
print(f.readline())
2.
次のコードの出力を選んでください。
# sample.txt
# ---
# Python is amazing!
# ---
with open('sample.txt') as f:
print(f.read(6))
3.
次のコードを実行したときの出力を選んでください。
# sample.txt
# ---
# Hello, World!
# ---
with open('sample.txt', 'r') as f:
print(f.read())
4.
次のコードの実行結果を選んでください。
# sample.txt
# ---
# Line1
# Line2
# ---
with open('sample.txt', 'r') as f:
print(f.read())
print(f.read())
5.
次のコードでf.readline()を使用した場合の出力を選んでください。
# sample.txt
# ---
# Hello, Python!
# Welcome to the world of programming.
# ---
with open('sample.txt', 'r') as f:
print(f.readline())
6.
次のコードの実行結果を選んでください。
# empty.txt は空のファイル
with open('empty.txt', 'r') as f:
print(f.read())
7.
次のコードの実行結果を選んでください。
# sample.txt
# ---
# Line1
# Line2
# ---
with open('sample.txt', 'r') as f:
for line in f:
print(line.strip())
8.
次のコードの実行結果を選んでください。
# sample.txt
# ---
# Hello, World!
# ---
with open('sample.txt', 'r') as f:
f.seek(7)
print(f.read(5))
9.
次のコードの実行結果を選んでください。
# sample.txt
# ---
# Python is fun.
# File Handling is useful.
# ---
with open('sample.txt', 'r') as f:
print(f.readlines()[-1])
10.
次のコードの実行結果を選んでください。
# empty.txt は存在しないファイル
try:
with open('empty.txt', 'r') as f:
print(f.read())
except FileNotFoundError as e:
print("File not found!")
11.
次のコードの実行結果を選んでください。
# sample.txt
# ---
# Line1
# Line2
# ---
with open('sample.txt', 'r') as f:
lines = f.readlines()
print(len(lines))
12.
次のコードにおいて、テキストファイルを読み込むための正しい方法を選んでください。
# sample.txt
# ---
# Hello, World!
# ---
13.
次のコードにおいて、file not foundエラーを回避する正しい方法を選んでください。
try:
with open('missing.txt', 'r') as f:
data = f.read()
except ??? as e:
print(f"Error: {e}")
14.
次のコードで、ファイルを行ごとにリストとして読み込む方法を選んでください。
# sample.txt
# ---
# Python is fun.
# Let's learn it!
# ---
with open('sample.txt', 'r') as f:
lines = f.???
print(lines)
15.
次のコードの実行結果を選んでください。
# sample.txt
# ---
# Python Programming
# File Handling
# ---
with open('sample.txt', 'r') as f:
print(f.read(15))
print(f.read())
16.
次のコードの実行結果を選んでください。
# sample.txt
# ---
# Hello, Python!
# ---
with open('sample.txt', 'r') as f:
for line in f:
print(line, end="")
17.
次のコードの実行結果を選んでください。
# sample.txt
# ---
# Line1
# Line2
# ---
with open('sample.txt', 'r') as f:
print(f.tell())
f.read(5)
print(f.tell())
18.
次のコードの実行結果を選んでください。
# sample.txt が空の場合
with open('sample.txt', 'r') as f:
data = f.read()
print(data)
19.
次のコードの実行結果を選んでください。
# sample.txt
# ---
# Line1
# Line2
# Line3
# ---
with open('sample.txt', 'r') as f:
print(f.read(5))
print(f.read(5))
20.
次のコードの実行結果を選んでください。
# sample.txt
# ---
# Line1
# Line2
# Line3
# ---
with open('sample.txt', 'r') as f:
print(len(f.read()))