0

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.

  • 1
    depends all on the `cwd` value of your launch config – rioV8 Mar 12 '23 at 20:35
  • In PyCharm the Run Configuration specifies a working directory. I don't know about VS Code. But in your script you can `print(os.getcwd())` to see what implicit assumptions about the environment your script is running under. – Peter Wood Mar 12 '23 at 20:39

1 Answers1

0

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__))
eloqz
  • 127
  • 2
  • 6
  • don't use the script path to save stuff. Use the current directory of the terminal, what if you have the script stored in a read only directory, or a directory from an other user – rioV8 Mar 12 '23 at 20:37
  • I agree it's preferable to use the cwd, but the question specifically mentioned the folder of the code. I will add a clarification to my answer – eloqz Mar 12 '23 at 20:40
  • only use fixed paths in the script if you want to save something in `TEMP`, and then you should use the environment variable and construct the file path, or use an argument for the location to store the result, only use the script file path if you need to READ stuff that is stored next to the script. – rioV8 Mar 12 '23 at 22:44