How do I write in a new text file every x lines? [python]

Awn

Let's say I have a list with 10,000 entries. The user inputs a number, say 10. What I need to do is write all of the entries in the list to .txt files, but only 10 to each file. So the program would write the first 10, and then create a new file, and write the next 10... etc.

Thanks

count = 0
ext = 0
path = 'New/' + str(ext) + '.txt'
open(path, 'a').close()
for line in flines:
    f = open(path, 'a')
    if count <= x:
        f.write(line)
        count += 1
    else:
        ext += 1
        path = 'New/' + str(ext) + '.txt'
    count += x
    f.close()

This is what I've tried, amongst some other solutions. I have a feeling that I'm missing something simple.

This is different to the other question pointed out above as that's talking about a text file and this is talking about a list.

taskinoor

Use a generator to split lines list like this:

>>> def splited_lines_generator(lines, n):
...     for i in xrange(0, len(lines), n):
...             yield lines[i: i + n]
... 
>>> for index, lines in enumerate(splited_lines_generator(['a', 'b', 'c', 'd', 'e'], 2)):
...     with open('new/' + str(index) + '.txt', 'w') as f:
...             f.write('\n'.join(lines))
... 

I have used ['a', 'b', 'c', 'd', 'e'] and 2 just as an example. After running this three files will be created, first two will contain two lines and last one will contain a single line (only e). Note that, directory new must be present before running this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I write in a new text file every x lines? [python]

From Dev

Python 2.7 - How do I remove lines in a text file longer than x amount?

From Dev

How do I delete multiple lines in a text file with python?

From Dev

Write text into first lines of every file

From Dev

How do I write a dictionary info a text file in this format? (Python)

From Dev

how do i write a text file to csv in python with consideration to columns

From Dev

Python - How do I write an array to a text file?

From Dev

Split Text File for Every x Lines

From Dev

How do I copy all the duplicate lines of a file to a new file in Python?

From Dev

Reverse block of text every x lines in python

From Dev

python 3.x : How to write/append text to a existing file as per user input in new line?

From Dev

How do I write only so many lines from a read file in python

From Dev

How do I write all lines from less to a file?

From Dev

How do I extract a range of lines from a text file using sed -n but in Python?

From Dev

Python Write to Text File Skip Bad Lines

From Dev

How do i upload text file to my ftp every minute?

From Dev

How to write text to every empty file

From Dev

How do I limit (or truncate) text file by number of lines?

From Dev

How do I count the number of blank lines in a text file?

From Dev

How do I grab the first 10% of lines in a text file?

From Dev

How do I write a manifest file for the new java security barriers

From Dev

How do i write a new line or \n into a file

From Dev

how to write multiple lines every time by columns to a file in python3

From Dev

How to write to a new line every time in python?

From Dev

How to write every line of a text file into new files and name them according to its content?

From Dev

How to write only new text to a text file

From Dev

Writing text to txt file in python on new lines?

From Dev

How can I write specific lines from a text area form to a text file? PHP

From Dev

How do I add text to every other item in a python list?

Related Related

  1. 1

    How do I write in a new text file every x lines? [python]

  2. 2

    Python 2.7 - How do I remove lines in a text file longer than x amount?

  3. 3

    How do I delete multiple lines in a text file with python?

  4. 4

    Write text into first lines of every file

  5. 5

    How do I write a dictionary info a text file in this format? (Python)

  6. 6

    how do i write a text file to csv in python with consideration to columns

  7. 7

    Python - How do I write an array to a text file?

  8. 8

    Split Text File for Every x Lines

  9. 9

    How do I copy all the duplicate lines of a file to a new file in Python?

  10. 10

    Reverse block of text every x lines in python

  11. 11

    python 3.x : How to write/append text to a existing file as per user input in new line?

  12. 12

    How do I write only so many lines from a read file in python

  13. 13

    How do I write all lines from less to a file?

  14. 14

    How do I extract a range of lines from a text file using sed -n but in Python?

  15. 15

    Python Write to Text File Skip Bad Lines

  16. 16

    How do i upload text file to my ftp every minute?

  17. 17

    How to write text to every empty file

  18. 18

    How do I limit (or truncate) text file by number of lines?

  19. 19

    How do I count the number of blank lines in a text file?

  20. 20

    How do I grab the first 10% of lines in a text file?

  21. 21

    How do I write a manifest file for the new java security barriers

  22. 22

    How do i write a new line or \n into a file

  23. 23

    how to write multiple lines every time by columns to a file in python3

  24. 24

    How to write to a new line every time in python?

  25. 25

    How to write every line of a text file into new files and name them according to its content?

  26. 26

    How to write only new text to a text file

  27. 27

    Writing text to txt file in python on new lines?

  28. 28

    How can I write specific lines from a text area form to a text file? PHP

  29. 29

    How do I add text to every other item in a python list?

HotTag

Archive