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