Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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

like image 793
Felix Farquharson Avatar asked Aug 17 '26 16:08

Felix Farquharson


1 Answers

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'
like image 92
Julien Avatar answered Aug 20 '26 07:08

Julien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!