Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

Change Path Python Created File

When I create a file like .txt or .db with Python code, the file is always created in root directory and not in my current directory. Does anyone have an idea of how can I change the path of a created file in Python with Visual Studio Code?

I know I can do this with os module or with adding code lines, but I want to set it up by default in Visual Studio.

This is my current configuration in launch.json

This is what I get when I debug with F5

question from:https://stackoverflow.com/questions/65857242/change-path-python-created-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In VS Code, when we run the code generation file, the VS Code terminal enters the project root directory by default (the command to run the file is executed here):

enter image description here

In VS Code, when we run the code generation file, the VS Code terminal enters the project root directory by default (the command to run the file is executed here):

Method 1:

Please "cd" to the folder you need before running the python script:

enter image description here

Method 2:

Please modify the debugging path to the file you need: please use the following settings in "launch.json": "cwd": "${fileDirname}", then click F5:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...