Sending directory to IPFS using Python

Hello, I am trying to upload a directory to ipfs using python request and am having trouble figuring out how to format my files parameter. Currently I have this:
files = {
“dir”: {
“test.txt”: “t”,
“subdir”: {
“subtest.txt”: “st”
response =, headers=headers, files=files)

This throws this error:

TypeError(“a bytes-like object is required, not ‘dict’”)

Is it possible to upload a directory and potentially subdirectories in one request??
If so what am I doing wrong?

Hi @confused_dev, welcome to our community!

Here’s a script I’ve built for uploading a folder to IPFS using python:

import requests
import os
import json

proj_id = '27h...'
proj_secret = '205...'
dir_name = '/Users/christian/Desktop/Infura/IPFS/folder_test/'
items = {}

for f in os.listdir(dir_name):
    item = open(dir_name + f, 'rb')
    items[f] = item

response ="",
                         auth=(proj_id, proj_secret),files=items)

# for printing the CIDs in the console:
dec = json.JSONDecoder()
i = 0
while i < len(response.text):
    data, s = dec.raw_decode(response.text[i:])
    i += s + 1
    print("%s: %s" % (data['Name'], data['Hash']))

The lastly printed CID in the console is the CID of the root folder.

Hope this helps!

1 Like