#10-1
with
open
(
'learning_python.txt'
)
as
lp:
l1
=
lp.read()
print
(l1)
for
i
in
lp:
print
(i)
l2
=
lp.readlines()
for
i
in
l2:
print
(i.strip())
for
i
in
l2:
#10-2
i1
=
i.replace(
'Python'
,
'Go'
)
print
(i1)
#10-3
name
=
input
(
'your name'
)
with
open
(
'name.txt'
,
'a'
)
as
fn:
fn.write(name)
#10-4
while
(
True
):
name
=
input
(
'your name:'
)
print
(
'hello'
,name)
with
open
(
'guest_book.txt'
,
'a'
)
as
gn:
gn.write(name)
#10-5
while
(
True
):
why
=
input
(
'why you like coding?'
)
with
open
(
'because.txt'
,
'a'
)
as
wn:
wn.write(why)
import
math
#10-6
a
=
input
(
'number:'
)
b
=
input
(
'number:'
)
try
:
a
=
int
(a)
b
=
int
(b)
except
ValueError
:
print
(
'please give me number!'
)
else
:
c
=
a
+
b
#10-7
while
(
True
):
a
=
input
(
'number:'
)
b
=
input
(
'number:'
)
try
:
a
=
int
(a)
b
=
int
(b)
except
ValueError
:
print
(
'please give me number!'
)
else
:
c
=
a
+
b
#10-8
try
:
with
open
(
'cat.txt'
)
as
cr:
cat
=
cr.read()
with
open
(
'dog.txt'
)
as
dr:
dog
=
dr.read()
except
FileNotFoundError
:
print
(
'file not found!'
)
else
:
print
(cat)
print
(dog)
#10-9
try
:
with
open
(
'cat.txt'
)
as
cr:
cat
=
cr.read()
with
open
(
'dog.txt'
)
as
dr:
dog
=
dr.read()
except
FileNotFoundError
:
pass
else
:
print
(cat)
print
(dog)
#10-10
try
:
with
open
(
'my_book.txt'
)
as
book:
b
=
book.read()
except
FileNotFoundError
:
print
(
'no book!'
)
else
:
c
=
b.count(
'the'
)
print
(c)
#10-11
import
json
number
=
int
(
input
(
'your favorite number:'
))
with
open
(
'number.txt'
,
'a'
)
as
n:
json.dump(number,n)
with
open
(
'number.txt'
)
as
r:
number
=
json.load(r)
print
(number)
#10-12
try
:
with
open
(
'number.txt'
)
as
num:
number
=
num.read()
except
FileNotFoundError
:
number
=
int
(
input
(
'your favorite number:'
))
with
open
(
'number.txt'
,
'a'
)
as
wr:
json.dump(number,wr)
print
(number)
else
:
print
(number)