Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...