Search code examples
pythonloopsline

How to save each line of a file to a new file (every line a new file) and do that for multiple original files


I have 5 files from which i want to take each line (24 lines in total) and save it to a new file. I managed to find a code which will do that but they way it is, every time i have to manually change the number of the appropriate original file and of the file i want to save it to and also the number of each line every time.

The code:

x1= np.loadtxt("x_p2_40.txt")
x2= np.loadtxt("x_p4_40.txt")
x3= np.loadtxt("x_p6_40.txt")
x4= np.loadtxt("x_p8_40.txt")
x5= np.loadtxt("x_p1_40.txt")    

with open("x_p1_40.txt", "r") as file:
 content = file.read()
 first_line = content.split('\n', 1)[0]
with open("1_p_40_x.txt", "a" ) as f : 
       f.write("\n")
with open("1_p_40_x.txt", "a" ) as fa :     
       fa.write(first_line)
        
print(first_line)

I am a beginner at python, and i'm not sure how to make a loop for this, because i assume i need a loop?

Thank you!


Solution

  • Since you have multiple files here, you could define their names in a list, and use a list comprehension to open file handles to them all:

    input_files = ["x_p2_40.txt", "x_p4_40.txt", "x_p6_40.txt", "x_p8_40.txt", "x_p1_40.txt"]
    
    file_handles = [open(f, "r") for f in input_files]
    

    Since each of these file handles is an iterator that yields a single line every time you iterate over it, you could simply zip() all these file handles to iterate over them simultaneously. Also throw in an enumerate() to get the line numbers:

    for line_num, files_lines in enumerate(zip(*file_handles), 1):
        out_file = f"{line_num}_p_40.txt"
        # Remove trailing whitespace on all lines, then add a newline
        files_lines = [f.rstrip() + "\n" for f in files_lines] 
        with open(out_file, "w") as of:
            of.writelines(files_lines)
    

    With three files:

    x_p2_40.txt:
    2_1
    2_2
    2_3
    2_4
    
    x_p4_40.txt:
    4_1
    4_2
    4_3
    4_4
    
    x_p6_40.txt:
    6_1
    6_2
    6_3
    6_4
    

    I get the following output:

    1_p_40.txt:
    2_1
    4_1
    6_1
    
    2_p_40.txt:
    2_2
    4_2
    6_2
    
    3_p_40.txt:
    2_3
    4_3
    6_3
    
    4_p_40.txt:
    2_4
    4_4
    6_4
    

    Finally, since we didn't use a context manager to open the original file handles, remember to close them after we're done:

    for fh in file_handles:
        fh.close()
    

    If you have files with an unequal number of lines and you want to create files for all lines, consider using itertools.zip_longest() instead of zip()