VS Code Python 3 doesn't actually create text file but it can still read from it

Question:

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

Asked By: Jason Ho

||

Answers:

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:

enter image description here

Answered By: ruddra

Try to run it with the Option ‘Run Current File in Interactive Window’.
It is worked for me.

You can find it here:

Answered By: Hasan