Reputation: 313
I just wanna know if this possible without using shutil to merge the files? but by using pathlib only.
my old code for merging files:
def combine(source: str, output: str),
with open(output, 'wb') as output_file:
for file in iglob(os.path.join(source, "*.mp4")):
print(f'Merging', file, end='\r')
with open(file, 'rb') as input_file:
shutil.copyfileobj(input_file, output_file)
This is what I tried with pathlib
def combine(source: str, output: str),
for file in Path(source).glob('*.mp4'):
print(f'Merging', file)
Path(output).write_bytes(file.read_bytes())
The problem is, it's not appending the bytes and combining to output file.
Upvotes: 0
Views: 158
Reputation: 8382
You should only open the output file once and then write the bytes of each file you read.
def combine(source: str, output: str),
print(f'Creating {output}')
with open(Path(output), 'wb') as outfile:
for file in Path(source).glob('*.mp4'):
print(f'Appending {file}')
outfile.write(file.read_bytes())
Upvotes: 2