python - VS Code Python 3 doesn't actually create text file but it can still read from it
问题描述
I've been using the stock Python IDLE for a while which has worked great, but I just downloaded VS Code and whilst it's a clear upgrade, I've noticed an issue with it, particularly with databases and text files.
I created a simple program which writes to a text file and then reads from it:
def writeText():
textFile = open("test.txt","w")
textFile.write("test")
textFile.close()
def readText():
textFile = open("test.txt","r")
store = textFile.readlines()
print(store)
writeText()
readText()
The first procedure should create 'test.txt' and write to it, whilst the second procedure will print the lines from it.
When I run the program in VS Code, the output is correct. However, when I check my file directory, there is no text file called 'test.txt', and yet it was able to output correctly.
When I run this code in the Python IDLE, it creates the text file correctly.
Can anyone help me get to the bottom of this? Thanks
解决方案
I have tried with this setup in launch.json
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/abc.py",
"console": "integratedTerminal"
},
where abc.py
is the file you have written, and it worked fine and created the file as expected:
推荐阅读
- javascript - 创建搜索栏
- sql - 更改了 Sql-Server 版本,无法创建带有发送电子邮件警报的作业
- r - 创建 100 个具有不同标题和文件名的 ggplots(行)以保存到文件夹的函数
- excel - 如何使用 VBA 识别 word 文档中的现有表格?
- java - junit.Assert.assertTrue 失败,但仅在我编译模块时?
- django - 如何解决此错误:分配前引用的 /templateName/ 局部变量“名称”处的 UnboundLocalError?
- javascript - Vee-validate 在没有 EventBus 的情况下独立验证来自父级的组件
- java - 对数组列表进行排序
- javascript - 在 svg-tooltip.html 中更改鼠标悬停时的条形颜色
- css - 如何在组件中导入带有变量的 css - SASS