Code
= open("data/pi_digits.txt",mode="r")
f = f.read()
contents print(contents)
= contents.rstrip()
contents print(contents)
f.close()
3.1415926535
8979323846
2643383279
3.1415926535
8979323846
2643383279
= open("data/pi_digits.txt",mode="r")
f = f.read()
contents print(contents)
= contents.rstrip()
contents print(contents)
f.close()
3.1415926535
8979323846
2643383279
3.1415926535
8979323846
2643383279
with open("data/pi_digits.txt",mode="r") as g:
for i in g:
print(i)
3.1415926535
8979323846
2643383279
from pathlib import Path
= Path("data/pi_digits.txt")
path = path.read_text()
f = f.splitlines()
f for i in f:
print(i)
3.1415926535
8979323846
2643383279
with open("data/write.txt",mode="a") as w:
"Π是无限不循环小数。")
w.write(with open("data/write.txt",mode="a") as w:
"fafhaofhaohfo") w.write(
exception
def division(dividend,divisor):
= dividend/divisor
answer return(answer)
4,2)
division(4,0)
division(
def division_(dividend,divisor):
try:
= dividend/divisor
answer except ZeroDivisionError:
print("You can't divide by 0!")
else:
return(answer)
finally:
print("OK")
4,2)
division_(4,0) division_(
ZeroDivisionError: division by zero