Hello everyone! I’m kinda new to Python and still learning, i have tried to search for solutions to my problem but everything i’ve tried so far has failed with different results and i finally decided that i had to give up and just ask. Sorry if it’s all too obvious I’m playing around with opening and reading some txt files and looping trough them, and this works exactly as intended:
with open("/path/to/folder/file1.txt") as lines:
for line in lines:
print(line)
But i was thinking if i could make this process less repetitive by somehow store the “/path/to/folder” in a variable and then pass the filename itself with a function, but my tries so far have failed pretty big
txt_file_folder = "/path/to/folder/"
def txt_file(file):
with open (txt_file_folder + file) as lines:
for line in lines:
print(line)
and then try to use it like this:
txt_file(file1.txt)
But from the error-messages i’ve got so far i can tell that it’s not really meant to use function-arguments like this. I have tried to convert it using str() and other methods i could think of but i still have no idea, maybe i’m looking for an impossible solution? I have been thinking about using format-strings to replace {filename} in the path above too somehow, but i’m not really sure it will be any less manual work than to just do it like my first example on all of the files. Hope anyone can lead me in the right direction or tell me what to search for, thanks