使用python将文件放在文件夹中

时间:2014-04-17 17:41:49

标签: python

我在网上抓了这个文件分割器,但我需要把文件块放到指定的文件夹中。代码是:

#define inputs
file = raw_input('enter file location:')
output = raw_input('enter file output location:')
chunk_size = raw_input('choose output chunk size(in bytes):')
# define the function to split the file into smaller chunks
def splitFile(inputFile,chunkSize):
    #read the contents of the file
    f = open(inputFile, 'rb')
    data = f.read()
    f.close()
# get the length of data, ie size of the input file in bytes
    bytes = len(data)
#calculate the number of chunks to be created
    noOfChunks= bytes/chunkSize
    if(bytes%chunkSize):
        noOfChunks+=1
#create a info.txt file for writing metadata
    f = open('info.txt', 'w')
    f.write(inputFile+','+'chunk,'+str(noOfChunks)+','+str(chunkSize))
    f.close()
    chunkNames = []
    for i in range(0, bytes+1, chunkSize):
        fn1 = "chunk%s" % i
        chunkNames.append(fn1)
        f = open(fn1, 'wb')
        f.write(data[i:i+ chunkSize])
        f.close()
#split file into chunks
splitFile(file,chunk_size)
#move chunks to output

所以我确定你可以看到,我已经制作了文件分割器,我只需要将文件块放在"输出"的目录中。变量。 有人可以帮帮我吗?!?

1 个答案:

答案 0 :(得分:1)

正在此行中创建新文件:

f = open(fn1, 'wb')

使用此行中创建的名称:

fn1 = "chunk%s" % i

这表明如果你想要一个目录,你可以使用这样的东西:

fn1 = "output/chunk%s" % i