0

I have a script which will read a .ini file. Into the .ini file there are multiple lines starting with a certain word: "word". (I do not know if every time I have more or less than 4 lines). after the "word" keyword there will be more paths to another files, something like this:

word = c:\Users\bgandul\PycharmProjects\trial01\notepad\trial01.txt
word = c:\Users\bgandul\PycharmProjects\trial01\notepad\trial02.txt
word = c:\Users\bgandul\PycharmProjects\trial01\notepad\trial03.txt
word = c:\Users\bgandul\PycharmProjects\trial01\notepad\trial04.txt

Inside of these files there are some words (does not matter); Let's suppose we have the following situation:

  • into the trial01.txt we have the following words: "Mary has"
  • into the trial02.txt we have "green apples"
  • into the trial03.txt we have "and"
  • into the trial04.txt we have "five oranges"

From command line interpreter I wish to be able to have the following output:

Mary has green apples and five oranges

Just to recall, does not matter how many paths there are. The script should concatenate the information from those trialxx.txt files (even there are only 2 files or 100 files)

Up to now I did the following:

def commandlineinitializer(path1):
    path1 = "C:\\Users\\bgandul\\PycharmProjects\\trial01\\config.ini"
    my_list = []
    word = 'word'
    with open(path1) as f:
        for line in f:
            if line.startswith(word):
                my_list.append(line)
    new_list = []
    for item in my_list:
        remove_prefix = item.removeprefix('word = ')
        remove_sufix = remove_prefix.removesuffix('\n')
        new_list.append(remove_sufix)
    print("new_list:", new_list)


argpath = sys.argv[1]

commandlineinitializer(argpath)

and now, the last list "new_list" I would like to parse it, and starting from here I am stucked.

I have read some answers from here: "https://stackoverflow.com/questions/15753701/how-can-i-pass-a-list-as-a-command-line-argument-with-argparse", but I cannot understand to much.

BGandul
  • 17
  • 5

1 Answers1

1

For the example data (one line per trialXY.txt file) the following should produce the required output:

def func(path):
    with open(path, "r") as file:
        print(file.read().rstrip(), end=" ")

WORD = "word"
def get_paths(ini_file_path):
    with open(ini_file_path, "r") as file:
        return [
            line.split("=")[1].strip() for line in file if line.startswith(WORD)
        ]

ini_file_path = "C:\\Users\\bgandul\\PycharmProjects\\trial01\\config.ini"
for path in get_paths(ini_file_path):
    func(path)

If you want to pack it into a script file script.py and do

$: python script.py <your ini-file path here>

then you could do

import sys

def func(path):
    with open(path, "r") as file:
        print(file.read().rstrip(), end=" ")

WORD = "word"
def get_paths(ini_file_path):
    with open(ini_file_path, "r") as file:
        return [
            line.split("=")[1].strip() for line in file if line.startswith(WORD)
        ]

ini_file_path = sys.argv[1]
for path in get_paths(ini_file_path):
    func(path)

or use argparse

from argparse import ArgumentParser, FileType

def func(path):
    with open(path, "r") as file:
        print(file.read().rstrip(), end=" ")

WORD = "word"
def get_paths(ini_file):
    return [
        line.split("=")[1].strip() for line in ini_file if line.startswith(WORD)
    ]

parser = ArgumentParser()
parser.add_argument("ini_file", type=FileType("r"))
args = parser.parse_args()

for path in get_paths(args.ini_file):
    func(path)
Timus
  • 10,974
  • 5
  • 14
  • 28
  • Thank you for your response. To be honest, I think I need something else, but my formulation was not so good. for example if there is a function with **one** parameter and the parameter have to be the paths from the config.ini file. How can be called a function for a certain numbers of times depending how many lines there are? thanks :) – BGandul Aug 08 '22 at 13:17
  • 1
    @BGandul So instead of printing the files on the prompt you want to call a function for each path in the ini-file (with the resp. path as argument)? – Timus Aug 08 '22 at 13:47
  • Yes, indeed. This is what I would like to do. – BGandul Aug 08 '22 at 16:37
  • @BGandul See my edit, but I'm still not sure if I understand correctly. Is the function you want to call defined inside the module or imported, or do you want to call a function outside of Python? – Timus Aug 09 '22 at 11:41