I'm trying to deal with txt files.
I have a 2D list called reps = [], len(reps) = 41, I also have a directory called replies, which contains 34 txt files.
I want to do like this:
reps[0][0]is the first line of thefile1.txtunder the replies directory,reps[0][1]is the second line etc.reps[1][0]is the first line of the secondfile2.txt- Until all
34 txtfiles have been replaced then it's done
how can I achieve this?
I would be very appreciate
Although if you indeed have 41 lists of strings in
reps, this will result in 41 files, not 34:If I misunderstood you and you actually really want to just replace exactly 34 files that already exist in that directory, you can always just add a conditional break in the beginning of the loop:
Note that
writelinesexpects each element in the provided iterable to provide its own line seperator, which is why the generator producesline + '\n'for every line.