以下代码经Python3.3测试。
1、
|
1
2
3
4
5
6
7
8
|
try
:
file
=
open
(
'config.ini'
,
'w'
)
print
(
"It's a text file"
,
file
=
file
)
except
IOError as err:
print
(
'File error: '
+
str
(err))
finally
:
if
'file'
in
locals
():
file
.close()
|
2、
|
1
2
3
4
5
|
try
:
with
open
(
'config.txt'
,
'w'
) as
file
:
print
(
"It's a text file"
,
file
=
file
)
except
IOError as err:
print
(
'File error: '
+
str
(err))
|
相关阅读:Python按行读取文件
***
本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1246965如需转载请自行联系原作者
RQSLT