使用python在Linux中分配特定大小的文件

时间:2015-08-21 21:27:51

标签: python linux file

我正在python中编写I / O密集型程序,我需要在硬盘上分配特定数量的存储空间。由于我需要尽可能快,我不想在循环中创建一个零(或虚拟)内容的文件。 python有没有任何库或方法,或者我是否必须在python中使用Linux命令?

实际上,我正在实现一个像BitTorrent一样的应用程序。在我的代码中,接收器将源文件的每个段存储在单独的文件中(源文件的每个段来自随机发送者)。最后,将合并所有单独的文件。这需要很多时间。

因此,我想提前分配一个文件,然后将源文件的每个接收段写入预先分配的文件中的偏移量。

def handler(self):    
    BUFFER_SIZE = 1024  # Normally 1024, but we want fast response
    # self.request is the TCP socket connected to the client
    data = self.request.recv(BUFFER_SIZE)
    addr = ..... #Some address

    details = str(data).split() 
    currentFileNum = int(details[0]) #Specifies the segment number of the received file.
    totalFileNumber = int(details[1].rstrip('\0')) # Specifies the total number of the segments that should be received.
    print '\tReceive: Connection address:', addr,'Current segment Number: ', currentFileNum, 'Total Number of file segments: ', totalFileNumber

    f = open(ServerThreadHandler.fileOutputPrefix + '_Received.%s' % currentFileNum, 'wb')
    data = self.request.recv(BUFFER_SIZE)
    while (data and data != 'EOF'):
        f.write(data)
        data = self.request.recv(BUFFER_SIZE)
    f.close()
    print "Done Receiving." ," File Number: ", currentFileNum
    self.request.sendall('\tThank you for data. File Number: ' + str(currentFileNum))
    ServerThreadHandler.counterLock.acquire()
    ServerThreadHandler.receivedFileCounter += 1
    if ServerThreadHandler.receivedFileCounter == totalFileNumber:
        infiles = []
        for i in range(0, totalFileNumber):
            infiles.append(ServerThreadHandler.fileOutputPrefix + '_Received.%s' % i)

        File_manipulation.cat_files(infiles, ServerThreadHandler.fileOutputPrefix + ServerThreadHandler.fileOutputSuffix, BUFFER_SIZE) # It concatenates the files based on their segment numbers. 
    ServerThreadHandler.counterLock.release()

1 个答案:

答案 0 :(得分:0)

通常(不仅在Python中,而且在操作系统级别),现代FS驱动程序支持sparse files,当您预先创建一个明显为零填充的文件,然后执行搜索和写入循环到您需要的位置时写一个特定的数据。

请参阅How to create a file with file holes?了解如何创建此类文件。