Reading the content from file in VSCODE IDE

Greetings:

I’ve a file is data_c.txt in files.py file and both are in same diectory/folder (C:\Users\Suneel\Desktop\python_work)

files.py
from pathlib import Path
path=Path(‘data_c.txt’)
content=path.read_text()
print(content)

When I’m running the above file its giving the below issue

KeyboardInterrupt

& C:/Users/Suneel/AppData/Local/Programs/Python/Python313/python.exe c:/Users/Suneel/Desktop/python_work/files.py
File “”, line 1
& C:/Users/Suneel/AppData/Local/Programs/Python/Python313/python.exe c:/Users/Suneel/Desktop/python_work/files.py
^
SyntaxError: invalid syntax

the below code also giving the same issue.
from pathlib import Path

path=Path(‘C:\Users\Suneel\Desktop\python_work\data_c.txt’)

content=path.read_text()

print(content)

Please do the needful.

If you’re seeing the prompt >>>, it means that you’re at Python’s interactive prompt.

What you’re typing there is for the system command line, which on Windows is in a Command Prompt window where the prompt would be something line C:\Users\Suneel>.

Also, the initial & that you’re typing is specific to Linux, so you shouldn’t be using it on Windows.

On Windows it’s recommended that you use the Python Launcher py:

py c:/Users/Suneel/Desktop/python_work/files.py

In the second piece of code, file paths that contain backslashes should use raw string literals:

path=Path(r'C:\Users\Suneel\Desktop\python_work\data_c.txt')

or doubled backslashes:

path=Path('C:\\Users\\Suneel\\Desktop\\python_work\\data_c.txt')

or, alternatively, forward slashes:

path=Path('C:/Users/Suneel/Desktop/python_work/data_c.txt')

Windows will happily accept forward slashes except in dialog boxes.

1 Like

Thanks Matthew Barnett, Appreciate your response!!

I’m still getting the issue, Can you please have a look.

You see the >>> in the Terminal pane? That tells you that it’s a Python prompt. You’re talking to Python, not the Windows command line.

Try typing:

quit()

in the Terminal pane. That should drop you to either the Windows Command Prompt or Windows Powershell. You can then enter:

py c:/Users/Suneel/Desktop/python_work/files.py

or whatever from there.

To open a Command Prompt window, press the Windows key on your keyboard, type “command prompt”, and press the Enter key.

1 Like

Do those files I can see in the File Explorer have extensions? I can see that “data_c.txt” does.

If they do all have extensions but they’re being hidden (which would mean that “data_c.txt” actually has 2 extensions?), I suggest you tell File Explorer to show them. That’s generally recommended.

1 Like

yes you are awesome… its completely file extension issue… thank you.