Jun-24-2017, 05:22 AM
(Jun-23-2017, 08:19 PM)Ofnuts Wrote: This is the purpose of "functions"
# define the function def copyFileLines(fromFileName,toFileName,start,end): linesToCopy=range(start,end) with open(fromFileName) as fromFile, open(toFileName, 'a') as toFile: for i, line in enumerate(fromFile, 1): if i in linesToCopy: toFile.write(line) # use it: fileName1='file1.txt' fileName2='file2.txt' fileName3='file3.txt' copyFileLines(fileName1,fileName3, w+16,d+30) copyFileLines(fileName2,fileName3, w+20,d+20) copyFileLines(fileName1,fileName3, w+36,d+52) copyFileLines(fileName2,fileName3, w+42,d+42) # etc...Note that you are opening/closing all the files each time, if they are big the performance is going to be awful. You can open the target file once for all before calling the functions (replace parameter with its name with a parameter that hold the open file object). There is likely a simpler logic (fixed intervals) that allows you top open the two source files once for all, especially if they are always read/copied in the forward direction. In which case your code would just be a short loop.
Files are small, how it possible to repeat those lines where "w" and "d" values are with fixed values?
Look my last post, there are same cycle every 4 blocks after #4 command line...