user3537613
user3537613

Reputation: 1

place files in folder with python

i grabbed this file splitter online but i need to place the files chunks into a specified folder. the code is:

#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

so as im sure you can see, i have the file splitter all made, i just need to have the file chunks placed in the directory of the "output" variable. can someone please help me out?!?

Upvotes: 0

Views: 178

Answers (1)

g.d.d.c
g.d.d.c

Reputation: 47978

The new files are being created in this line:

f = open(fn1, 'wb')

Which uses the name created in this line:

fn1 = "chunk%s" % i

Which suggests that if you want a directory included you'd use something like this:

fn1 = "output/chunk%s" % i

Upvotes: 1

Related Questions