Felix Farquharson
Felix Farquharson

Reputation: 352

in python what is the / operator doing in the following code

i ran the following code and got no errors

def setup_static_routes(app):
    app.router.add_static('/static/',
                          path=PROJECT_ROOT / 'static',
                          name='static')

but if i run

PROJECT_ROOT="a"
path=PROJECT_ROOT / 'static'

i get the following error

Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    path=PROJECT_ROOT / 'static'
TypeError: unsupported operand type(s) for /: 'str' and 'str'

what is the / operator doing in the first example that doesn't throw an error

code found at https://docs.aiohttp.org/en/v3.0.1/tutorial.html

Upvotes: 3

Views: 59

Answers (1)

Julien
Julien

Reputation: 15070

This script probably assumes a variable of type Path, in which case / is defined as path concatenation. Try this:

from pathlib import Path
PROJECT_ROOT = Path("a")
path=PROJECT_ROOT / 'static'

Upvotes: 5

Related Questions