-6

I have a text file lets say FileA.txt. I want to split this txt file at every 25 lines so Line 0-25 will be File1.txt, Lines 26-50 will be File2.txt and so on.

I have attempted to follow:

Splitting large text file into smaller text files by line numbers using Python

But have not had much luck. My python skills are pretty basic and low-level.

When I ran it I got the following error:

"", line 1 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Then I added an r like so with open(r'C:\Users and now I get PermissionError: [Errno 13] Permission denied: 'C:\Users\joker\Desktop\LiveStream_Videos'

9
  • 2
    Where are your efforts? Can you please show us and explain what you have tried to do already also please be specific, are you attempting to do with with python 2.x or python 3? Commented Dec 13, 2018 at 2:50
  • I am trying to do it with python 3. I have tried to follow some examples on here but have not had much luck.Basically I have FileA.txt and I am trying to make File1.txt containing Lines 0-25 then File2.txt 26-50 and so on. Commented Dec 13, 2018 at 2:54
  • 1
    Please include your effort in the question, as well as sample input and output Commented Dec 13, 2018 at 2:56
  • I strongly suggest you edit your original question and try to put as much information as possible, you're not going to get any help if you don't show us you can help yourself. Commented Dec 13, 2018 at 2:59
  • 1
    Where is your example code? You've just pasted a SO link, we can assume that the answer to that question works so in order to help you we would need to see your implementation. Commented Dec 13, 2018 at 3:05

1 Answer 1

3

You could try chunking every 25 lines, then write them each out to separate files:

def chunks(l, n): """Chunks iterable into n sized chunks""" for i in range(0, len(l), n): yield l[i:i + n] # Collect all lines, without loading whole file into memory lines = [] with open('FileA.txt') as main_file: for line in main_file: lines.append(line) # Write each group of lines to separate files for i, group in enumerate(chunks(lines, n=25), start=1): with open('File%d.txt' % i, mode="w") as out_file: for line in group: out_file.write(line) 

Note: The chunking recipe from How do you split a list into evenly sized chunks?.

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

4 Comments

I tried using def chunks(l, n): """Chunks iterable into n sized chunks""" for i in range(0, len(l), n): yield l[i:i + n] # Collect all lines from file lines = [] with open('C:\LinkTest.txt') as f: for line in f: lines.append(line) # Write each group of lines to separate files for i, group in enumerate(chunks(lines, n=25), start=1): with open('C:\File%d.txt' % i, mode="w") as f: for line in group: f.write(line)
However, it appears to run but the files werent made
@abat Try the recent edit, it works for me.
thanks for your help it worked!!!! Thank you so much

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.