Search code examples
pythonpython-3.xvisual-studio-codepycharmimagegrab

Vs Code saving image in different folder than Pycharm


I've been using Pycharm for some time, so with this code

from PIL import ImageGrab

im = ImageGrab.grab((0, 0, 250, 250))

im.save('teste.png', 'PNG')

I would expect this file to be saved in the same folder as the code, like it happens on Pycharm, Like this

But when I put the same code in VS Code, I get this result, Saved in another folder, It's saved some folders before

I've tried looking for something different in the files or the files configuration from both apps, but couldn't find it. Is it because of a configuration that is different in the settings of VS Code and Pycharm? Is there a way to make it behave as it would in Pycharm, or change the way it works in VS Code? Appreciate all help, thanks.


Solution

  • EDIT: As mentioned in the comments, it's better to use the working directory of your runtime environment to define where files defined by local paths will be saved. The question was marked as duplicate so you can find the answer there. To use the current directory of the code, see the original answer below:

    If you want to save in the same folder as the code, it's better to use an absolute path rather than a relative path.

    To get the absolute path of the folder the code is in you can use:

    import os
    os.path.dirname(os.path.abspath(__file__))