Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Read 2 files and output merged lines to 1 file

data = data2 = "";

# Reading data from file1
with open('sample1.txt') as fp:
    data = fp.read()

# Reading data from file2
with open('sample2.txt') as fp:
    data2 = fp.read()

# Merging 2 files
# To add the data of file2
# from next line


with open ('sample3.bat', 'a') as fp:
    fp.writelines("KeyHunt-Cuda -m address --coin btc --range " + data + ":" + data2 + "13zb1hQbWVsc2S7ZTZnP2G4undNNpdh5so" '\n')

Output on above code

KeyHunt-Cuda -m address --coin btc --range aaaa
bbbb
cccc:ddd
eee
fff13zb1hQbWVsc2S7ZTZnP2G4undNNpdh5so

File 1

aaaa
bbbb
cccc

File 2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ddd
eee
fff

I hoped the output would be like

KeyHunt-Cuda -m address --coin btc --range aaaa:ddd 13zb1hQbWVsc2S7ZTZnP2G4undNNpdh5so
KeyHunt-Cuda -m address --coin btc --range bbbb:eee 13zb1hQbWVsc2S7ZTZnP2G4undNNpdh5so
KeyHunt-Cuda -m address --coin btc --range cccc:fff 13zb1hQbWVsc2S7ZTZnP2G4undNNpdh5so

How should I change the code?

>Solution :

Use readlines() to read each file into a list of lines. Then loop over the lines to produce the output you want.

# Reading data from file1
with open('sample1.txt') as fp:
    data = fp.readlines()

# Reading data from file2
with open('sample2.txt') as fp:
    data2 = fp.readlines()

with open ('sample3.bat', 'a') as fp:
    for line1, line2 in zip(data, data2):
        fp.write(f"KeyHunt-Cuda -m address --coin btc --range {line1.strip()}:{line2.strip()} 13zb1hQbWVsc2S7ZTZnP2G4undNNpdh5so\n")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading