Home > OS >  IsADirectoryError: [Errno 21] Is a directory: '/' explanation
IsADirectoryError: [Errno 21] Is a directory: '/' explanation

Time:06-11

I am getting this error from this code.

import os
import requests
import shutil


path = "/Users/mycode/Documents/API upload/"


api_endpoint = "xxxxxx"


files = {
    'file': open(p,'rb') for p in os.path.abspath(path)
}

for file in os.path.abspath(path):
    response = requests.post(url=api_endpoint, files=files)
    if response.status_code == 200:
        print(response.status_code)
        print("success!")
    else:
        print("did not work")


IsADirectoryError: [Errno 21] Is a directory: '/'

^ what does this error mean? I tried googling it but still do not understand in my case. It has something to do with the paths but not sure why.

anything helps!

CodePudding user response:

for p in os.path.abspath(path)

doesn't do what you think it does.

It does not iterate over all files in a given directory. Use os.listdir for that. You can combine the directory path and the filename inside the directory using os.path.join. The pathlib module has an IMHO simpler to use / higher level interface to all of this.

What your code does is iterate over all characters in the string returned by os.path.abspath(path). And the first character is /. Which you then try to open as a file. And that doesn't work, because / is a directory.

CodePudding user response:

You might want to consider doing this in chunks because if your directory contents are very large, you could run out of file descriptors.

Something like this should work:

from requests import post
from glob import glob
from os.path import join, isfile

DIR = '/Users/mycode/Documents/API upload/'
CHUNK = 10
API_ENDPOINT = '...'

filelist = [filename for filename in glob(join(DIR, '*')) if isfile(filename)]

for idx in range(0, len(filelist), CHUNK):
    files = [('file', open(fn, 'rb')) for fn in filelist[idx:idx CHUNK]]
    post(API_ENDPOINT, files=files).raise_for_status()
    for _, fd in files:
        fd.close()

Note:

For improved efficiency you should consider multithreading for this

  • Related