config parse

📅 2024-06-05T19:20:51.232Z
👁️ 20 katselukertaa
🔓 Julkinen


import sys
import os

def parse_filename(filename):
    # Remove extension from filename
    filename_without_extension = os.path.splitext(filename)[0]
    # Find the index of the last underscore
    last_underscore_index = filename_without_extension.rfind('_')
    if last_underscore_index != -1:
        # Extract the part of the filename before the last underscore
        parsed_name = filename_without_extension[:last_underscore_index]
        return parsed_name
    else:
        return None

def update_config_file(filename, parsed_name):
    config_file_path = "/path/to/config/file.config"  # Update with the actual path to the config file
    # Read the existing content of the config file
    with open(config_file_path, 'r') as f:
        lines = f.readlines()
    
    # Find and update the line with File_Desc
    for i, line in enumerate(lines):
        if line.startswith("File_Desc="):
            lines[i] = "File_Desc=" + parsed_name + "\n"
            break
    
    # Write the modified content back to the config file
    with open(config_file_path, 'w') as f:
        f.writelines(lines)

if __name__ == "__main__":
    # Check if filename is provided as command line argument
    if len(sys.argv) != 2:
        print("Usage: python script.py filename")
        sys.exit(1)
    
    filename = sys.argv[1]
    parsed_name = parse_filename(filename)
    
    if parsed_name:
        update_config_file(filename, parsed_name)
        print("Config file updated successfully.")
    else:
        print("Filename format is incorrect.")