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.