0

Good afternoon, I ran into a little problem. I want to create a file in a folder that doesn't exist. How can I best do this?

When I specify the path itself with non-existent folders, I get a FileNotFoundError. I also tried to create folders manually and then write the file to them, but I also get the same error.

Thanks in advance for the answer to everyone who was not indifferent

with open('/data/file.txt', 'w', encoding='UTF-8') as file:
    file.write(some_data)

Regardless of whether the folder exists, I get the error

Decision - Creating a missing directory / file structure - Python

  • How have you tried to create the folders? "Regardless of whether the folder exists, I get the error" sounds like a different problem which is answered here https://stackoverflow.com/questions/2967194/open-in-python-does-not-create-a-file-if-it-doesnt-exist – JeffUK Dec 10 '20 at 09:53
  • I used the open method with the path – Илья Николаев Dec 10 '20 at 09:56

0 Answers0