Leearn2303
Leearn2303

Reputation: 141

concat lines from several .txt file using python?

Im using python 3.6 on windows 10, i have a task to take data from 1.txt and concat them with some string and put the results on 2.txt file .

this is my code:

full_url = "https://mysite/images/pic_person/small/"

#file read from
pic_name = open("test.txt","r")
#file write to it 
full_name = open("full_name.txt","a")
while True:
    line = pic_name.readline()
    link = line+full_url
    print(link)
    full_name.write(link)
    if ("" == line):
        print("file finished")
        break;
pic_name.close()
full_name.close()

after executing the code it gives me this result:

p100003.jpg
https://mysite/images/pic_person/small/p100026.jpg
https://mysite/images/pic_person/small/p100951.jpg
https://mysite/images/pic_person/small/p100970.jpg
https://mysite/images/pic_person/small/p101144.jpghttps://mysite/images/pic_person/small/https://mysite/images/pic_person/small/

and except results will be like this :

https://mysite/images/pic_person/small/p100026.jpg
https://mysite/images/pic_person/small/p100951.jpg
https://mysite/images/pic_person/small/p100970.jpg
https://mysite/images/pic_person/small/p101144.jpg

the file test.txt contains these lines :

p100026.jpg
p100951.jpg
p100970.jpg
p101144.jpg

Upvotes: 0

Views: 61

Answers (5)

Gwang-Jin Kim
Gwang-Jin Kim

Reputation: 9930

  • Prefer with open(file, mode=["r", "w", "w+"]) as <file_handler>: over open() and <fhandler>.close(). In case an error occurs, the file stream gets closed automatically in the with version while not in the latter version. So with is much safer and robust.
  • Use for loop over the file handler - then no explicit test for end of file (eof) is needed. And it reads line by line. Your test with line == "" might be okay for this case - but it might not be okay for another case. Using the for-loop is the more universal solution. In addition - and more importantly - the memory is occupied only by one line of the fin file at any time point. So memory footprint stays small.

So the most pythonic way for this problem is:

infile = "test.txt"
outfile = "full_name.txt"
full_url = "https://mysite/images/pic_person/small/"

with open(infile, "r") as fin:
    with open(outfile, "a") as fout:
        for line in fin:
            fout.write(full_url + line + "\n")
            # or: print(full_url + line, end="\n", file=fout)

If you want the verbose printing out like in your example, do:

infile = "test.txt"
outfile = "full_name.txt"
full_url = "https://mysite/images/pic_person/small/"

with open(infile, "r") as fin:
    with open(outfile, "a") as fout:
        for line in fin:
            result_line = full_url + line
            print(result_line, end="\n", file=fout)
            print(result_line)
print("file finished")

Upvotes: 0

xio
xio

Reputation: 640

You can use this method

full_url = "https://mysite/images/pic_person/small/"

with open("test.txt") as pic_name:
    links = [full_url + line.rstrip() for line in pic_name]
    
    with open("full_name.txt","w") as name:
        name.write("\n".join(links))

or

with open("test.txt") as pic_name:
    with open("full_name.txt","a") as name:
        for line in pic_name:
            name.write(full_url + line.rstrip() + "\n")

Output in full_name.txt

https://mysite/images/pic_person/small/p100026.jpg
https://mysite/images/pic_person/small/p100951.jpg
https://mysite/images/pic_person/small/p100970.jpg
https://mysite/images/pic_person/small/p101144.jpg

Upvotes: 0

ferdy
ferdy

Reputation: 5034

You may try this.

full_url = "https://mysite/images/pic_person/small/"

outfile = "fullname.txt"
infile = "test.txt"

with open(outfile, "a") as w:
    with open(infile, "r") as r:
        for line in r:  # read each line in the input file
            w.write(f"{full_url}{line}")  # write to output file

Input file, test.txt

p100026.jpg
p100951.jpg
p100970.jpg
p101144.jpg

Output file fullname.txt

https://mysite/images/pic_person/small/p100026.jpg
https://mysite/images/pic_person/small/p100951.jpg
https://mysite/images/pic_person/small/p100970.jpg
https://mysite/images/pic_person/small/p101144.jpg

Upvotes: 0

Insight Habor
Insight Habor

Reputation: 81

to append file on the bottom you need to use 'a' flag in the open file and put "\n" as enter

full_url = "https://mysite/images/pic_person/small/"

# file read from test.txt
f = open("test.txt", "r")

# iterating each file on pict and write it
with open("text2.txt", "a") as file_object:
    for item in f:
        file_object.write(full_url+item.splitlines()[0]+"\n")

f.close()

Upvotes: 1

DorElias
DorElias

Reputation: 2313

you can replace the line

full_name.write(link)

with

full_name.writeline(link)

Alternativly you can write a "new line character" yourself (think of it as simulating pressing enter)

full_name.write(link + '\r\n')

Upvotes: 0

Related Questions