Open file

代码
#! /usr/bin/python
import os,sys

try:
fsock
= open("D:/SVNtest/test.py", "rb")
except IOError:
print "The file don't exist, Please double check!"
exit()
print 'The file mode is ',fsock.mode
print 'The file name is ',fsock.name
P
= fsock.tell()
print 'the postion is %d' %(P)
for EachLine in fsock:
print EachLine
fsock.close()
  •  文件对象的 tell 方法告诉你在被打开文件中的当前位置
  •  seek 方法在被打开文件中移动到另一个位置,第二个参数指出第一个参数是什么意思:0 表示移动到一个绝对位置 (从文件起始处算起),1 表示移到一个相对位置 (从当前位置算起),还有 2 表示相对于文件尾的位置。

关闭文件

print '==========check the file status============'
S1
= fsock.closed
if True == S1:
print 'the file is closed'
else:
print 'The file donot close'
Work for fun,Live for love!
原文地址:https://www.cnblogs.com/allenblogs/p/1824773.html