0

I have two lists. One has the urls and the other the filenames. The filename should change with the for loop for each video. I get the following error:

open(f'{k[1:]}.mp4', 'wb').write(r.content) ,
FileNotFoundError: [Errno 2] No such file or directory: '10560.mp4'
def download_cdn():

    links = get_cdn_link()
    file_name = get_link()

    for i in links:
        for k in file_name:
            r = requests.get(i, allow_redirects=True)
            open(f'{k[1:]}.mp4', 'wb').write(r.content)

file_name = ['/yxz/10560', '/yxz/10561', '/yxz/10562', '/yxz/10578', '/asd/10565', '/asd/10564']
KA N
  • 61
  • 8
  • 1
    Do the directories `yxz` and `asd` already exist in the directory where the code is being executed? – Andy May 26 '20 at 20:57
  • no but I cut the first string to remove the "/" with [1:] – KA N May 26 '20 at 20:58
  • 1
    When writing to a file path if the directory does not already exists, [you need to create the directory](https://stackoverflow.com/questions/23793987/write-file-to-a-directory-that-doesnt-exist) – DarrylG May 26 '20 at 21:02
  • `[1:]` only removes the first character here, so because there's a second slash it treats what comes before it as a directory. – Andy May 26 '20 at 21:05
  • in concert with @DarrylG question, does the directories "yxz" and "asd" already exist? – Akib Rhast May 26 '20 at 21:05
  • Thanks now it works. I removed both slashes – KA N May 26 '20 at 21:11

1 Answers1

-1

Place {k[1:]} outside of the quotes. Python is treating it as a literal string instead of a variable. Do this instead:

open(str(k[1:]) + '.mp4', 'wb').write(r.content)

or use .format()

open('{}.mp4'.format(k[1:]), 'wb').write(r.content)