完成功能:
从指定位置读文件到控制台
#! /usr/bin/python
# coding=utf- 方法一、
try:
f = open ('/root/python/file/001.txt','r' ) print(f.read())
finally:
if f:
f.close() 方法二、
with open('/root/python/file/001.txt','r') as f:
print (f.read()) 方法三、
f = open ('/root/python/file/001.txt','r' )
for line in f.readlines():
print (line.strip())
效果:
[root@localhost python]# ./004_IO-test.py