1

So this program is designed to get scrape memes from reddit and download them to a specific directory, However every time I run this code I get an error 13 to the specified directory that the memes are supposed to be saved to. please help me I wasted 3 days on this error

Heres my code:

import os
import praw
import requests
from datetime import datetime

def downloadMemes():
    #gets reddit credentials
    r = praw.Reddit(client_id = 'iV5Qtn1sC7-AvS26PfYRJw', 
                         client_secret = '3ZMMo2kKwN0a0uG2mly6GnN8rSmkPQ', 
                         user_agent = 'ytbot')

    #checks integrity of output folders
    if os.path.exists("memes"):
        pass
    if not os.path.exists("memes"):
        os.mkdir("memes")

    if os.path.exists("newMemes"):
        pass
    if not os.path.exists("memes"):
        os.mkdir("newMemes")

    #gets subreddit and number of posts depending on the day of the week
    dt = datetime.now()
    day = dt.weekday()
    if day == 0:
        subreddit = r.subreddit('Memes')
        posts = subreddit.hot(limit=9)
    if day == 1:
        subreddit = r.subreddit('dankmemes')
        posts = subreddit.hot(limit=9)
    if day == 2:
        subreddit = r.subreddit('AnarchyChess')
        posts = subreddit.hot(limit=9)
    if day == 3:
        subreddit = r.subreddit('ProgrammerHumor')
        posts = subreddit.hot(limit=9)
    if day == 4:
        subreddit = r.subreddit('school_memes')
        posts = subreddit.hot(limit=9)
    if day == 5:
        subreddit = r.subreddit('wholesomememes')
        posts = subreddit.hot(limit=9)
    if day == 6:
        subreddit = r.subreddit('AnimalMemes')
        posts = subreddit.hot(limit=9)
    
    #Downloads the posts
    for post in posts:
        url = (post.url)
        file_name = url.split("/")
        file_name = file_name[-1]
        print(file_name)
        r = requests.get(url)

        with open("memes/" + file_name,"wb") as f:
            f.write(r.content)

Here's the error I'm getting as you can see its error 13 permission denied

Traceback (most recent call last):
  File "C:\Users\LoneWanderer\source\repos\YT shorts bot\main.py", line 6, in <module>
    downloadMemes()
  File "C:\Users\LoneWanderer\source\repos\YT shorts bot\program_assets\memeDownloader.py", line 56, in downloadMemes
    with open("memes/" + file_name,"wb") as f:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: 'memes/'

I'm not sure if this is necessary for anyone who can help me debug but here's a screenshot of the directory tree

The Directory tree

What I tried:


I tried replacing with a for loop and I also tried running it on a Linux Virtual Machine but with both of these options I got the exact same error

  • Welcome to StackOverflow! This error means that **there is a problem with file permissions**. I think it should be something simple. Is your file open in another program, so python can't open it for writing? **Log out/restart your computer to close all programs** and try again. – Jurakin Feb 11 '23 at 17:25
  • Does this answer your question? [Errno 13 Permission denied Python](https://stackoverflow.com/questions/41910583/errno-13-permission-denied-python) – Jurakin Feb 11 '23 at 17:27

0 Answers0