addet mergefiles to python scripts
This commit is contained in:
parent
cfe5af52c4
commit
671349c13c
1 changed files with 30 additions and 0 deletions
30
mergefiles.py
Normal file
30
mergefiles.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
import os
|
||||
|
||||
def merge_files(file_paths):
|
||||
# Dictionary to hold file contents by extension
|
||||
files_by_extension = {}
|
||||
|
||||
# Read each file and group contents by extension
|
||||
for file_path in file_paths:
|
||||
_, ext = os.path.splitext(file_path)
|
||||
if ext not in files_by_extension:
|
||||
files_by_extension[ext] = []
|
||||
with open(file_path, 'r') as file:
|
||||
files_by_extension[ext].append(file.read())
|
||||
|
||||
# Write merged contents to new files
|
||||
for ext, contents in files_by_extension.items():
|
||||
merged_file_path = f'merged{ext}'
|
||||
with open(merged_file_path, 'w') as merged_file:
|
||||
for content in contents:
|
||||
merged_file.write(content)
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Example usage: specify the file paths you want to merge
|
||||
file_paths = [
|
||||
'/test/first.csv',
|
||||
'/test/second.csv',
|
||||
'/test/third.csv'
|
||||
]
|
||||
merge_files(file_paths)
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue