python文件操作

提供个人写的文件操作python脚本。

个人觉得不管哪个语言删除、移动都是最常用的文件操作,遗憾的是见到的语言文件库基本都不提供。不过也可以理解,这类文件操作效率太差。库的设计者指望用户使用read和write完成这类操作,效率由用户保证。

文件IO操作尤其需要注意效率,而提高效率最重要的是使用缓冲。一个字节一个字节读写完成文件操作的版本效率低的令人震惊。我已经将这个版本注释,读者有兴趣的话可以参考对比。

又一次重复发明*,虽然不值得提倡,不过动手实践倒是纠正了一些文件移动操作常识性误解。另外提供测试程序。欢迎拍砖。


# -*- coding: cp936 -*-
# All rights reserved.

# Written by soliddream <soliddream at 163.com>

"""
wrap file operate
"""


def FileDelete(fileObject,position,length):    
    fileSize = FileSize(fileObject)    
    if(fileSize<position+length):
        print ‘error.string deleted exceed limit when execute FileDelete‘
        return   
    
    fileObject.seek(position)
    deletedStr = fileObject.read(length)
    movedStr = fileObject.read(fileSize-position-length)
    fileObject.seek(position)
    fileObject.write(movedStr)
    fileObject.truncate(fileSize-length)
    return deletedStr
    """
    anotherFileObject = open(fileObject.name, ‘rb‘)
    anotherFileObject.seek(position+length)
    fileObject.seek(position)

    while(1):
        byte = anotherFileObject.read(1)
        if not byte:
            break
        fileObject.write(byte)
    fileObject.truncate(anotherFileObject.tell()-length)
    anotherFileObject.close()
    """    

def FileMove(fileObject,position,length,newPosition):    
    fileSize = FileSize(fileObject)
    if((fileSize<position+length) or (fileSize<newPosition)        or position==newPosition or position<0):
        print ‘error.FileMove Invalid argument‘
        return
    fileObject.seek(position)
    strmoved = fileObject.read(length)
    
    if(position<newPosition):
        lenDomino = newPosition-position-length+1
        fileObject.seek(position+length)
        strDomino = fileObject.read(lenDomino)
        fileObject.seek(position)
        fileObject.write(strDomino)
        fileObject.write(strmoved)
    else:
        lenDomino = position-newPosition
        fileObject.seek(newPosition)
        strDomino = fileObject.read(lenDomino)
        fileObject.seek(newPosition)
        fileObject.write(strmoved)
        fileObject.write(strDomino)
	"""
    anotherFileObject = open(fileObject.name, ‘rb‘)
    anotherFileObject.seek(position)
    strmoved = anotherFileObject.read(length)

    forNumber = 0
    if(position<newPosition):
        anotherFileObject.seek(position+length)
        fileObject.seek(position)
        forNumber = newPosition-position
    else:
        anotherFileObject.seek(newPosition)
        fileObject.seek(newPosition+length)
        forNumber = position-newPosition
    for i in range(0,forNumber):        
        byte = anotherFileObject.read(1)        
        fileObject.write(byte)

    fileObject.seek(newPosition)
    fileObject.write(strmoved)
    anotherFileObject.close()
    """

def FileSearch(fileObject,subString, beginPosition=0):
    fileObject.seek(beginPosition)
    fileString = fileObject.read();
    foundPosition = fileString.find(subString)
    if foundPosition!=-1:
        foundPosition += beginPosition
    return foundPosition

def FileSize(fileObject):
    originPosition = fileObject.tell()
    fileObject.seek(0, os.SEEK_END)
    fileSize = fileObject.tell()
    fileObject.seek(originPosition)
    return fileSize

def testMyFileFuction():
    print ‘-----------------------‘
    print ‘test FileDelete‘
    string = StringIO()
    string.write(‘0123456789‘)
    FileDelete(string,3,3)
    string.seek(0)
    if string.read()==‘0126789‘:
        print ‘FileDelete OK‘
    else:
        print ‘FileDelete Failed‘
    
    print ‘-----------------------‘
    print ‘test FileMove‘
    string.seek(0)
    string.write(‘0123456789‘)
    FileMove(string,3,3,0)
    string.seek(0)
    fileMoveResult1=string.read()
    string.seek(0)
    string.write(‘0123456789‘)
    FileMove(string,3,3,7)
    string.seek(0)
    fileMoveResult2 = string.read()
    if fileMoveResult1 == ‘3450126789‘ and fileMoveResult2 == ‘0126734589‘:
        print ‘FileMove OK‘




python文件操作

上一篇:U-boot, linux kernel, rootfs 理解


下一篇:移动应用可以通过微信沟通接口连接公众号 微信涨粉多了一个新通道