Search code examples
pythonvisual-studio-codecmddirectoryruntime-error

VS Code isn't identifying files in the same directory


enter image description hereI was running a simple python program in VS Code which basically opens a file and prints out the contents in upper case. But the program is not able to identify the text file('mbox-short.txt') which is in the same directory.

The problem exists only when I run the program in VS Code terminal. The Code runs fine when I use windows command prompt.

I have attached the screenshot of the code executed on the terminal in VS Code and on CMD. You can see the code running fine in CMD.enter image description here

The Code is given below!

fname = input("Enter the file name: ")
try:
    fhand = open(fname)
    for line in fhand:
        print(line.upper())
except:
    print("Invalid file name!")[![enter image description here][1]][1]

Solution

  • The current working directory in cmd is D:\Python\files, while it's D:\Python in VS Code. If you use the folder files as cwd, the result is exactly as the same as the one executed in external shell:

    enter image description here