user1821176
user1821176

Reputation: 1191

Splitting file into smaller files by lines

I am trying to figure out a way to split a big txt file with columns of data into smaller files for uploading purposes. The big file has 4000 lines and I wondering if there is a way to divide it into four parts such as

file 1 (lines 1-1000)

file 2 (lines 1001-2000)

file 3 (lines 2001-3000)

file 4 (lines 3001-4000)

I appreciate the help.

Upvotes: 0

Views: 1091

Answers (3)

MakeCents
MakeCents

Reputation: 764

I like Aleksander Lidtke's, but with a for loop and a pop() twist for fun. I also like to maintain some of the files original naming when I do this, since it is usually to multiple files. So I added the name "split" in it.

with open('Data.txt','r') as f:
    lines = f.readlines()

limit=1000
for o in range(len(lines)):
    if lines!=[]: 
        with open(f.name.split(".")[0] +"_" + str(o) + '.txt','w') as NewFile:
                for i in range(limit):
                    if lines!=[]:NewFile.write(lines.pop(0))

Upvotes: 0

Aleksander Lidtke
Aleksander Lidtke

Reputation: 2926

This works (you could implement a for rather than a while loop but it makes little difference and does not assume how many files will be necessary):

with open('longFile.txt', 'r') as f:
    lines = f.readlines()

threshold=1000
fileID=0
while fileID<len(lines)/float(threshold):
    with open('fileNo'+str(fileID)+'.txt','w') as currentFile:
        for currentLine in lines[threshold*fileID:threshold*(fileID+1)]:
            currentFile.write(currentLine)
        fileID+=1

Hope this helps. Try to use open in a with block as suggested in python docs.

Upvotes: 2

scohe001
scohe001

Reputation: 15456

Give this a try:

fhand = open(filename, 'r')
all_lines = fhand.readlines()

for x in xrange(4):
    new_file = open(new_file_names[x], 'w')
    new_file.write(all_lines[x * 1000, (x + 1) * 1000])

Upvotes: 0

Related Questions