파일 라인별로 나누기

Published by onesixx on

https://intrepidgeeks.com/tutorial/python-divides-large-text-files-into-multiple-small-files
https://gist.github.com/maximpertsov/7011378282ac0b1d873d

파일갯수를 정해, Line별로 나누기

# coding:utf-8
import os

def cutFile(fileNm):
    with open(fileNm, 'r') as f:
        lines = f.read().splitlines()
        n = len(lines)
        print(f"All Lines: {str(n)} ")
        dividedFiles = int(input(""))
        numOfLines = n//dividedFiles + 1
        print(
            f"Divided Fiels: {str(dividedFiles)}, Lines per file: {str(numOfLines)} ")
        [sub_filename, extname] = os.path.splitext(fileNm)
        for i in range(dividedFiles):cut
            subFileNm = sub_filename+"_part"+str(i+1)+extname
            with open(subFileNm, "w", encoding='utf-8') as destFileData:
                if(i == dividedFiles-1):   # Last File
                    for eachLine in lines[numOfLines*i:]:
                        destFileData.write(eachLine + '\
')
                else:           # 1~ File
                    for eachLine in lines[numOfLines*i:numOfLines*(i+1)]:
                        destFileData.write(eachLine + '\
')
cutFile('../app/data/mobi.json.test.txt')

Categories: Python Basic

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x