domingo, 10 de noviembre de 2024

reading a file

import os

import sys


def search_key_value(file_path, search_key):

    # Open and read the file safely using a context manager

    with open(file_path, 'r') as file:

        lines = file.readlines()


    # Create a dictionary from the parsed list

    parsed_dict = {line.strip().split(',')[0]: line.strip().split(',')[1] for line in lines}


    # Return the value associated with the search key if it exists

    return parsed_dict.get(search_key)


# Example usage:

file_path = '/var/www/html/fwlist.conf'

sound_folder = '/var/lib/asterisk/sounds/en/custom/'

search_key = sys.argv[1]


file = search_key_value(file_path, search_key)


if file:

    print(f"The value for key '{search_key}' is: {file}")

    # Check if the corresponding file exists

    file_path = sound_folder + file

    print(1 if os.path.isfile(file_path) else 0)

else:

    print(f"Key '{search_key}' not found.")

    print(0)

#############

849,hello
829,goodbye
809,version_2


No hay comentarios:

Publicar un comentario