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

abat
  • 1
  • 3
  • 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? – Madison Courto Dec 13 '18 at 02: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. – abat Dec 13 '18 at 02:54
  • 1
    Please include your effort in the question, as well as sample input and output – Andreas Dec 13 '18 at 02: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. – Madison Courto Dec 13 '18 at 02:59
  • Question has been edited and I have pasted what I have tried. – abat Dec 13 '18 at 03:01
  • 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. – Madison Courto Dec 13 '18 at 03:05
  • I'm sorry, but there is not enough information here for me to answer. Are you having trouble understanding? Is there an error that occurred when you ran the code? – Pika Supports Ukraine Dec 13 '18 at 03:07
  • I tried using split in linux and when it made the files it didnt split it at the beginning of every line. So I am trying to do it using Python and havent been able to find any samples – abat Dec 13 '18 at 03:17
  • Possible duplicate of [Splitting large text file into smaller text files by line numbers using Python](https://stackoverflow.com/questions/16289859/splitting-large-text-file-into-smaller-text-files-by-line-numbers-using-python) – Madison Courto Dec 13 '18 at 03:18

1 Answers1

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?.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
  • 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) – abat Dec 13 '18 at 03:40
  • However, it appears to run but the files werent made – abat Dec 13 '18 at 03:40
  • @abat Try the recent edit, it works for me. – RoadRunner Dec 13 '18 at 03:42
  • thanks for your help it worked!!!! Thank you so much – abat Dec 13 '18 at 03:43