Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot define path: TypeError: unsupported operand type(s) for /: 'str' and 'str'

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Just wrap the entire path in the f-string:

with open(f'insects/{dragonfly}.pdf', 'wb') 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading