test
- 📅 2024-06-05T20:53:35.537Z
- 👁️ 67 katselukertaa
- 🔓 Julkinen
import sys
import os
def parse_filename(filename):
# Parse filename up to before the last underscore
parsed_name = filename.rsplit('_', 1)[0]
# Check if 'ACK' or 'CAN' is in the last 10 characters of the filename
ack_or_can = 'ACK' if 'ACK' in filename[-10:] else ('CAN' if 'CAN' in filename[-10:] else '')
return parsed_name, ack_or_can
def update_config_file(config_file, parsed_name, ack_or_can):
# Open the config file and read its contents
with open(config_file, 'r') as f:
lines = f.readlines()
# Find and update the File_Desc field
for i, line in enumerate(lines):
if line.startswith('File_Desc='):
lines[i] = f'File_Desc={parsed_name}\n'
break
# Update the ACK_OR_CAN field
lines.append(f'ACK_OR_CAN={ack_or_can}\n')
# Write the updated contents back to the config file
with open(config_file, 'w') as f:
f.writelines(lines)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py filename")
sys.exit(1)
filename = sys.argv[1]
parsed_name, ack_or_can = parse_filename(filename)
# Assuming the config file is in the same directory with the same name but with .config extension
config_file = os.path.splitext(filename)[0] + '.config'
update_config_file(config_file, parsed_name, ack_or_can)
print(f"Config file '{config_file}' updated successfully.")