-
-
Save papposilene/cb456e01281590332318 to your computer and use it in GitHub Desktop.
Python script to split a large of exported scrobbles file into multiple and smaller .scrobbler.log files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import os | |
class FileSplitter: | |
def __init__(self): | |
self.parse_args(sys.argv) | |
@staticmethod | |
def run(): | |
splitter = FileSplitter() | |
splitter.split() | |
def split(self): | |
file_number = 1 | |
line_number = 1 | |
print "Splitting %s into multiple files with %s lines" % (os.path.join(self.working_dir, self.file_base_name+self.file_ext), str(self.split_size | |
)) | |
out_file = self.get_new_file(file_number) | |
out_file.write("#AUDIOSCROBBLER/1.0\n#TZ/UTC\n#CLIENT/Rockbox h3xx 1.1\n") | |
for line in self.in_file: | |
out_file.write(line) | |
line_number += 1 | |
if line_number == self.split_size: | |
out_file.close() | |
file_number += 1 | |
line_number = 1 | |
out_file = self.get_new_file(file_number) | |
out_file.write("#AUDIOSCROBBLER/1.0\n#TZ/UTC\n#CLIENT/Rockbox h3xx 1.1\n") | |
out_file.close() | |
print "Created %s files." % (str(file_number)) | |
def get_new_file(self,file_number): | |
"""return a new file object ready to write to""" | |
new_file_name = "%s%s.scrobbler.log" % (self.file_base_name, str(file_number)) | |
new_file_path = os.path.join(self.working_dir, new_file_name) | |
print "creating file %s" % (new_file_path) | |
return open(new_file_path, 'w') | |
def parse_args(self,argv): | |
"""parse args and set up instance variables""" | |
try: | |
self.split_size = 9000 | |
if len(argv) > 2: | |
self.split_size = int(argv[2]) | |
self.file_name = argv[1] | |
self.in_file = open(self.file_name, "r") | |
self.working_dir = os.getcwd() | |
self.file_base_name, self.file_ext = os.path.splitext(self.file_name) | |
except: | |
print self.usage() | |
sys.exit(1) | |
def usage(self): | |
return """ | |
Split a large file into many smaller files with set number of rows. | |
Usage: | |
$ python file_splitter.py <file_name> [row_count] | |
row_count is optional (default is 1000) | |
""" | |
if __name__ == "__main__": | |
FileSplitter.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment