I have a simple code line in Python, use too many + is not nice
filename = path + '/static/' + folder + '/' + name + 'html'
Can be like that in php
$filename = "$path/static/$folder/{$name}html"
So how we write shorten in Python?
What you want is join the path components. To do this you can use the os.path.join() function which uses os.sep as separator.
>>> import os
>>> path = 'workspace'
>>> folder = 'stackoverflow'
>>> name = 'layout'
>>> os.path.join(path, 'static', folder, name, 'html')
'workspace/static/stackoverflow/layout/html'
You can also use PurePath if you are using Python 3.4 or newer.
from pathlib import PurePath
p = PurePath(path)
filename = str(p / static / folder / name / 'html')
Demo:
>>> from pathlib import PurePath
>>> p = PurePath(path)
>>> filename = str(p / 'static' / folder / name / 'html')
>>> filename
'workspace/static/stackoverflow/layout/html'
You can do this in python which would be much more readable,
filename = "{}/static/{}/{}html".format(path, folder, name)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With