I found how to run the code in the interpreter and ran into another problem. If you run the code in the interpreter, the current interpreter directory is 'C: \ Program Files \ Python36'. And I would like it to automatically go to the directory where the code was launched from. Is it possible?

Specific problem:
If you run a program through the console which is laid out below, it works , and if through the interpreter, it does not.

name = 'file.txt' with open(name) as fin1: for line in fin1: print(line) 

Solution is in the program code to change the current directory: import o

 import os import sys print(sys.argv[0]) # имя запускаемого файла path = sys.argv[0][:sys.argv[0].rindex('\\')] # выделяем только путь к файлу os.chdir(path) name = 'file.txt' with open(name) as fin1: for line in fin1: print(line) 

So everything works correctly. But it is still not convenient, because I don’t always run my code, and it’s not good to embed such changes in someone else’s. So is it possible to automatically go to the directory where the code was launched from?

  • one
    Related question: Current directory in Python - jfs
  • one
    In your particular case, you can try setting a working directory in VS settings (set to $(SolutionDir) ). In general, see the link above for how to get files, the path to which you only know about your module (not relevant to the current working directory) without chdir() (if I see open('file.txt') in the code, I expect file.txt to be in the current working directory, not the directory where the script itself lives - these can be different folders). - jfs
  • Please tell me exactly where I should write $ (SolutionDir) - Kto To
  • I do not know specific menus, look in the project properties (right click on the name) -> Settings. - jfs
  • one
    here the default folder is changed in stackoverflow.com/questions/6135480/… - Andrio Skur

0