Added the program splitter code.
This commit is contained in:
1 parent
199d622c24
commit
55177b1444
1 file changed
+64
@@ -0,0 +1,64 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def split_file_by_key(input_file, output_dir):
|
||||||
|
os.makedirs(output_dir, exist_ok=True)
|
||||||
|
|
||||||
|
current_output = None
|
||||||
|
key = "O"
|
||||||
|
|
||||||
|
with open(input_file, "r", encoding="utf-8") as infile:
|
||||||
|
for line in infile:
|
||||||
|
if line.startswith(key):
|
||||||
|
|
||||||
|
if current_output:
|
||||||
|
current_output.write("%\n")
|
||||||
|
current_output.close()
|
||||||
|
|
||||||
|
filename = line[:6].strip()
|
||||||
|
output_path = os.path.join(output_dir, f"{filename}.txt")
|
||||||
|
|
||||||
|
current_output = open(output_path, "w", encoding="utf-8")
|
||||||
|
current_output.write("%\n")
|
||||||
|
|
||||||
|
if current_output:
|
||||||
|
current_output.write(line)
|
||||||
|
|
||||||
|
if current_output:
|
||||||
|
current_output.write("%\n")
|
||||||
|
current_output.close()
|
||||||
|
|
||||||
|
print(f"Finished: {input_file}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
input_dir = input("Input folder: ").strip().strip("'\"")
|
||||||
|
|
||||||
|
if not os.path.isdir(input_dir):
|
||||||
|
print(f"Folder not found: {input_dir}")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
master_output_dir = os.path.join(input_dir, "Split_Output")
|
||||||
|
os.makedirs(master_output_dir, exist_ok=True)
|
||||||
|
|
||||||
|
for root, dirs, files in os.walk(input_dir):
|
||||||
|
if os.path.abspath(root).startswith(os.path.abspath(master_output_dir)):
|
||||||
|
continue
|
||||||
|
|
||||||
|
for filename in files:
|
||||||
|
if not filename.lower().endswith(".pgm"):
|
||||||
|
continue
|
||||||
|
|
||||||
|
input_file = os.path.join(root, filename)
|
||||||
|
|
||||||
|
program_output_dir = os.path.join(
|
||||||
|
master_output_dir,
|
||||||
|
os.path.splitext(filename)[0]
|
||||||
|
)
|
||||||
|
|
||||||
|
split_file_by_key(
|
||||||
|
input_file,
|
||||||
|
program_output_dir
|
||||||
|
)
|
||||||
|
|
||||||
|
print("All files processed.")
|
||||||
Reference in new issue
Block a user