简体   繁体   中英

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

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM