Problem writing multiple lines to a file in python

I want to download some specific links (image urls) in a txt file (or any file where all links can be listed below each other).

I get them, but the code wrtite each link at the top is different and at the end it remains only a link :( Also I don't want to repeat the url

def dlink(self, image_url):
        r = self.session.get(image_url, stream=True)
        with open('Output.txt','w') as f:
            f.write(image_url + '\n')

      

+3


source to share


4 answers


Edit

Huge mistake of my part as this is a method and you will call it multiple times, if you open it in write mode ("w") or similar, it will overwrite the existing file if the file exists. So, if you use the "a" method, you can see that:

Opens a file for adding. The file pointer is at the end of the file if the file exists. That is, the file is in append mode. If the file does not exist, it creates a new file for writing.

Another problem in the image_url file is the list, so you need to write it line by line:



def dlink(self, image_url):
        r = self.session.get(image_url, stream=True)
        with open('Output.txt','a') as f:
            for url in list(set(image_url)):
                f.write(image_url + '\n')

      

another way to do it:

your_file = open('Output.txt', 'a')
r = self.session.get(image_url, stream=True)
for url in list(set(image_url)):
  your_file.write("%s\n" % url)
your_file.close() #dont forget close it :)

      

+2


source


the file open mode is wrong, the "w" mode makes this file overwritten every time you open it, rather than being attached to it. replace it with "a" mode.



you can see this fooobar.com/questions/61025 / ... for more details

+2


source


The problem is that opening a file with mode 'w'

truncates any existing file. Instead, change 'w'

to 'a'

. This will open the existing file for writing, but add instead of truncating.

More fundamentally, the problem may be that you open the file over and over in a loop. This is very inefficient. The only time the approach you take can be really helpful is if your program is approaching the OS's limited open file limit. If this is not the case, I would recommend putting the loop inside the block with

, keeping this mode as 'w'

, since you only open the file once and pass the open file to your function dlink

.

+2


source


Open the file with option w

, overwrite the file, if it opens, use the mode a

to add data to the existing file.

Try:

import requests
from os.path import splitext


# use mode='a' to append result without erasing filename
def dlink(url, filename, mode='w'):
    r = requests.get(url)
    if r.status_code != 200:
        return
    # here the link is valid
    with open(filename, mode) as desc:
        desc.write(url)


def dimg(img_url, img_name):
    r = requests.get(img_url, stream=True)
    if r.status_code != 200:
        return
    _, ext = splitext(img_url)
    with open(img_name + ext, 'wb') as desc:
        for chunk in r:
            desc.write(chunk)


dlink('https://image.flaticon.com/teams/slug/freepik.jpg', 'links.txt')
dlink('https://image.flaticon.com/teams/slug/freepik.jpg', 'links.txt', 'a')

dimg('https://image.flaticon.com/teams/slug/freepik.jpg', 'freepik')

      

0


source







All Articles