fileHandle = open ( 'test.txt', 'a' ) fileHandle.write ( 'nnBottom line.' ) fileHandle.close()
fileHandle = open ( 'test.txt' ) print fileHandle.read() fileHandle.close()
fileHandle = open ( 'test.txt' ) print fileHandle.readline() # "This is a test." fileHandle.close()
fileHandle = open ( 'test.txt' ) fileList = fileHandle.readlines()<DIV></DIV> for fileLine in fileList: print '>>', fileLine fileHandle.close()
fileHandle = open ( 'test.txt' ) garbage = fileHandle.readline() fileHandle.readline() # "Really, it is."fileHandle.close()
fileHandle = open ( 'test.txt' ) garbage = fileHandle.readline() fileHandle.seek ( 0 ) print fileHandle.readline() # "This is a test." fileHandle.close()
fileHandle = open ( 'test.txt' ) print fileHandle.readline() # "This is a test." print fileHandle.tell() # "17" print fileHandle.readline() # "Really, it is."
fileHandle = open ( 'test.txt' ) print fileHandle.read ( 1 ) # "T" fileHandle.seek ( 4 ) print FileHandle.read ( 1 ) # " "(原文有错)
fileHandle = open ( 'testBinary.txt', 'wb' ) fileHandle.write ( 'There is no spoon.' ) fileHandle.close() fileHandle = open ( 'testBinary.txt', 'rb' ) print fileHandle.read() fileHandle.close()
import os
import stat
import time<DIV></DIV>
fileStats = os.stat ( 'test.txt' )
fileInfo = {
'Size' : fileStats [ stat.ST_SIZE ],
'LastModified' : time.ctime ( fileStats [ stat.ST_MTIME ] ),
'LastAccessed' : time.ctime ( fileStats [ stat.ST_ATIME ] ),
'CreationTime' : time.ctime ( fileStats [ stat.ST_CTIME ] ),
'Mode' : fileStats [ stat.ST_MODE ]
}
for infoField, infoValue in fileInfo:
print infoField, ':' + infoValue
if stat.S_ISDIR ( fileStats [ stat.ST_MODE ] ):
print 'Directory. '
else:
print 'Non-directory.'
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 ] ):
import 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.' import os for fileName in os.listdir ( '/' ): print fileName
import os os.mkdir ( 'testDirectory' )
import os os.rmdir ( 'testDirectory )
import os
os.makedirs ( 'I/will/show/you/how/deep/the/rabbit/hole/goes' )
os.makedirs ( 'I/will/show/you/how/deep/the/rabbit/hole/goes' )
import os os.removedirs ( 'I/will/show/you/how/deep/the/rabbit/hole/goes'
import fnmatch
import os
for fileName in os.listdir ( '/' ):
if fnmatch.fnmath ( fileName, '*.txt' ):
print open ( fileName ).read()
elif fnmatch.fnmatch ( fileName, '*.exe' ):
print fileName
import fnmatch
import os
for fileName in os.listdir ( '/' ):
if fnmatch.fnmatch ( fileName, '?.txt' ):
print 'Text file.'
import fnmatch
import os
import re
filePattern = fnmatch.translate ( '*.txt' )
for fileName in os.listdir ( '/' ):
if re.match ( filePattern, fileName ):
print 'Text file.'
import glob for fileName in glob.glob ( '*.txt' ): print 'Text file.'
import glob for fileName in glob.glob ( '[0-9].txt' ): print filename
import pickle fileHandle = open ( 'pickleFile.txt', 'w' ) testList = [ 'This', 2, 'is', 1, 'a', 0, 'test.' ] pickle.dump ( testList, fileHandle ) fileHandle.close()
import pickle fileHandle = open ( 'pickleFile.txt' ) testList = pickle.load ( fileHandle ) fileHandle.close()
import pickle
fileHandle = open ( 'pickleFile.txt', 'w' )
testList = [ 123, { 'Calories' : 190 }, 'Mr. Anderson', [ 1, 2, 7 ] ]
pickle.dump ( testList, fileHandle )
fileHandle.close()
import pickle
fileHandle = open ( 'pickleFile.txt' )
testList = pickle.load ( fileHandle )
fileHandle.close()
import cPickle fileHandle = open ( 'pickleFile.txt', 'w' ) cPickle.dump ( 1776, fileHandle ) fileHandle.close()
import StringIO fileHandle = StringIO.StringIO ( "Let freedom ring" ) print fileHandle.read() # "Let freedom ring." fileHandle.close()
import cStringIO fileHandle = cStringIO.cStringIO ( "To Kill a Mockingbird" ) print fileHandle.read() # "To Kill a Mockingbid" fileHandle.close()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有