f=open('test'.txt','w')//写入文件
f=open('test.txt','a')//追加写入文件
read()一次性读取
readline()读取一行
readlines()读取一行,并保留为一个list里面
seek(0)文件指针指向开头
tell()显示目前指针位置
os和stat模块获取文件:
stat:
import os
import stat
fileStats = os.stat ( 'test.txt' )
fileMode = fileStats [ stat.ST_MODE ]
if stat.S_ISREG ( fileStats [ stat.ST_MODE ] ):
print 'Regular file.'
elif stat.S_ISDIR ( fileStats [ stat.ST_MODE ] ):
print 'Directory.'
elif stat.S_ISLNK ( fileStats [ stat.ST_MODE ] ):
print 'Shortcut.'
elif stat.S_ISSOCK ( fileStats [ stat.ST_MODE ] ):
print 'Socket.'
elif stat.S_ISFIFO ( fileStats [ stat.ST_MODE ] ):
print 'Named pipe.'
elif stat.S_ISBLK ( fileStats [ stat.ST_MODE ] ):
print 'Block special device.'
elif stat.S_ISCHR ( fileStats [ stat.ST_MODE ] ):
print 'Character special device.'
os.path:
fileStats = 'test.txt'
if os.path.isdir ( fileStats ):
print 'Directory.'
elif os.path.isfile ( fileStats ):
print 'File.'
elif os.path.islink ( fileStats ):
print 'Shortcut.'
elif os.path.ismount ( fileStats ):
print 'Mount point.'