After a while of figuring out how many lines made a file a certain size and manually entering the start and ending combinations to split the dictionary into smaller files, I decided that it was time to learn a way to make Python split the file based on size during generation. Below you can see the function that I created to perfrom this. I tried to comment this one a bit better to explain in more detail. I also added a path variable that outputs all the files to a single folder called Output. This can easily be changed for user input to select an output location at a later time. You will also notice that before the loop creates a new file part, I reopen the previous file and delete the last character. This was the easiest way I could think of cleaning up the last new line character. Each file part would have a blank last line and I needed an quick way to clean this up. This worked well for what I needed at the time.
import os
import time
path = os.getcwd() + '/Output/'
if os.path.exists(path) == False: # Checks if the folder exists and if not creates it
os.mkdir(path)
def customSplit():
#Gather and set parameters
size = int(input('Enter the length of passcode: '))
start = int(input('Enter starting combination: '))
end = int(input('Enter last combination: '))
file_size = float(input('Enter the size in MB: '))
total_size = 0
part = 1
max_size = file_size * 1000000 # Multiples the size entered by the user to get bytes
print()
filename = path + str(start).zfill(size) + '_to_' + str(end).zfill(size) + '_Dictionary_' + str(part) + '.txt'
f = open(filename, "w", newline="\n", encoding="utf-8")
for num in range(start, end): # Start the loop to iterate through all combinations
if total_size >= max_size: # If the total size is greater than or equal to the size set by the user
f.close() # Close the current part of the file
f = open(filename, "a", newline="\n", encoding="utf-8") #open the file to clean up the extra line at the end
f.seek(0, os.SEEK_END) #move the pointer to the end of the file
f.truncate(f.tell() - 1) #truncate the file by one character which deletes the last new line character
f.close() # close the file and begin next file part
part += 1 # Increment the part number
filename = path + str(start).zfill(size) + '_to_' + str(end).zfill(size) + '_Dictionary_' + str(part) + '.txt' # Create a new filename
f = open(filename, "a", newline="\n", encoding="UTF-8") # Open the new part of the file
total_size = 0 # Reset the total size to 0
f.write(str(num).zfill(size) + "\n") #write the current combination to the file followed by a newline
total_size += len(str(num).zfill(size)) + 1 # Increment the total size by the size of the current line plus one for the newline character
f.write(str(end).zfill(size)) #write the final combination without a newline
f.close() #close the final file
print()
print('Exported files to: ' + path)