0

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.

2
  • 2
    What are you looking for here? Iterate through the lines. count while doing so. Output to buffer. at line 1001 close file writer. rinse repeat. whathaveyoutried.com Commented Jul 30, 2013 at 18:35
  • 3
    If you use linux you could just use the simple split command like this: split -l 1000 file.txt. Commented Jul 30, 2013 at 18:45

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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]) 

Comments

0

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)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.