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

前端 未结 7 750
野的像风
野的像风 2020-11-28 08:50

I have a text file say really_big_file.txt that contains:

line 1
line 2
line 3
line 4
...
line 99999
line 100000

I would like to write a Py

7条回答
  •  甜味超标
    2020-11-28 09:16

    Set files to the number of file you want to split the master file to in my exemple i want to get 10 files from my master file

    files = 10
    with open("data.txt","r") as data :
        emails = data.readlines()
        batchs = int(len(emails)/10)
        for id,log in enumerate(emails):
            fileid = id/batchs
            file=open("minifile{file}.txt".format(file=int(fileid)+1),'a+')
            file.write(log)
    

提交回复
热议问题