I’ve been facing an issue with defining a directory to use for the function open(). Unfortunately, it tells me that there is a TypeError: unsupported operand type(s) for /: 'str' and 'str'. How can I define in what folder to put the file without using a forward slash? I tried a backslash, but that prompted another error. I checked out a similar post’s solution, but I got an error saying SyntaxError: expected ':'.
import pathlib
from pathlib import Path
dragonfly = input("name?")
directory = pathlib.Path("insects")
directory.mkdir(exist_ok=True)
r = "this is a test"
with open("insects" / f'{dragonfly}.pdf', 'wb') as file:
file.write(r)
>Solution :
Just wrap the entire path in the f-string:
with open(f'insects/{dragonfly}.pdf', 'wb')