Prince Birring Prince Birring - 2 months ago 9
Python Question

Trying to join a two separate file but the program is not working

# join files interactively
import os
def menu():
print("welcome to file joiner")
print()
print("you will need to specify the original file name")
filename =input("Enter the original name:")
return filename
def main():
try:
filename = menu()
metaFile = filename + ".meta"

infile = open(metaFile, "r")
metaInfo = infile.readlines()
splitsFilePrefix = metaInfo[0] + strip()
bufferSize = metaInfo[1].strip()
bufferSize = int(bufferSize)
splits = metaInfo[2] + strip()
splits = int(splits)

try:
os.remove("new" + filename)
except:
pass

outfile = open("new" + filename, "ab")
infile = open(splitsFilePrefix + str(0), "rb")
buffer = infile.read(bufferSize)
i = 0

while len(buffer):
outfile.write(buffer)
print(".", end="")
i = i+1
if i == splits:
break
splitsFilePrefix = metaInfo[0].strip()
splitsFilePrefix = splitsFilePrefix + str(i)
infile = open(splitsFilePrefix,"rb")
buffer = infile.read(bufferSize)

infile.close()
outfile.close()
print()
print("Files rejoined, File name is :new" + filename)
except:
print("An error Occured")
main()

Answer

You haven't said what's wrong. Nobody can help make it work how it's supposed to, if we don't know what it's supposed to do.

  • I skimmed it looking for syntax errors, can't see any.

  • I saw + str(0) and wondered if it only opened the first file. Turns out it doesn't, it opens other files as well.

  • I saw splitsFilePrefix = splitsFilePrefix + str(i) and thought it might make filenames xyz1, xyz12, xyz123, but no that's handled by re-reading the meta file every time.

  • I saw the big try: {everything} except: {error} code which will be trapping every possible helpful error message and replacing it with an unhelpful one. That's unhelpful.

  • I determined that the intent of the program is:

.

give it a filename, it opens:
filename.meta

which is structured:

filename_prefix
buffer_size
number_of_parts

then loops through
filename_prefix0
filename_prefix1
filename_prefix..

and reinvents the wheel of `cat` and `copy` and puts all the contents in
newfilename

I saw this:

i = i+1
if i == splits: 
    break

and thought it might be an off-by one error, breaking before reading the last segment. No, it counts 0,1,2 instead of 1,2,3, so that should work.

My current guess is:

    while len(buffer):

        buffer = infile.read(bufferSize)

At the end of the first file, it will read nothing, the buffer will be empty, the loop will stop, and it won't proceed to any remaining files.

This is because you can't repeatedly read in from one file and step through all the files in the same loop.

Redesign to be something like:

outfile = open("new" + filename, "ab")

for i in range(splits):
    with open(splitsFilePrefix + str(i), 'rb') as infile:
        while len(buffer):
            outfile.write(buffer)
            print(".", end="")
            buffer = infile.read(bufferSize)

All untested, except for some quick testing to verify the buffer thing, so ymmv.