0

When I run the code in terminal outside PyCharm's IDE:

    directory = "./exports./images"

    if not os.path.isdir(directory):
        os.mkdir(directory)

    with open(os.path.join(directory, "test"), "wb") as f:
        f.write(b"\xff")

It works perfectly fine; however, when running it in PyCharm, it gives FileNotFoundError. When running this in PyCharm:

    directory = "./exports"

    if not os.path.isdir(directory):
        os.mkdir(directory)

    with open(os.path.join(directory, "test"), "wb") as f:
        f.write(b"\xff")

It works perfectly fine. Why does the first one not work in PyCharm, and the second one does? And why does the first one work outside of PyCharm?

fAXvw5Le
  • 11
  • 2
  • in terminal what is your current working folder (type : pwd) ? that's the reason probably it works – eshirvana Apr 30 '22 at 16:20
  • 1
    Does this answer your question? [PyCharm current working directory](https://stackoverflow.com/questions/34304044/pycharm-current-working-directory) – bad_coder Apr 30 '22 at 16:21
  • No, files and directories work perfectly fine in the same directory. I'm trying to create a nested directory, e.g. /exports/images doesn't work, and /exports/ does. – fAXvw5Le Apr 30 '22 at 16:22
  • 1
    Why are you adding the second dot in `"./exports./images"`? – bad_coder Apr 30 '22 at 16:29
  • For some reason, if /exports/ already exists and /images/ doesn't, the code that previously didn't work somehow does work. – fAXvw5Le Apr 30 '22 at 16:33
  • os.mkdir(directory) throws an exception when called on folder which doesn't have existing parent directory. you can find this in official docs: https://docs.python.org/3/library/os.html – Danil Kononyhin Apr 30 '22 at 16:34
  • 1
    Do the answers to this [question](https://stackoverflow.com/questions/273192/how-can-i-safely-create-a-nested-directory) help at all? – quamrana Apr 30 '22 at 16:35
  • No, I actually didn't even know what the actual problem was. The same thing happens outside of PyCharm, and I wasn't aware. – fAXvw5Le Apr 30 '22 at 16:36
  • You can use os.makedirs to create nested directories. You can do like is this answer: https://stackoverflow.com/a/273227/9041483 – Danil Kononyhin Apr 30 '22 at 16:38

0 Answers0