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?
$(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) withoutchdir()(if I seeopen('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