skip to Main Content
  • Every time i try running my program in an IDE: nothing happens
  • In console, it just runs the code but does not print anything.
  • I have tried changing the directory
  • I have tried making file at different locations
    When i try running the code in terminal it says: no such file or directory

I used:

'C:\Users\Asus-ss\new.py': [Errno 2] No such file or directory".

VSCODE-CODE:

f = open('itsme.txt','r')
f2 = f.read()
print(f2)
f.close()

CONSOLE

f = open('itsme.txt','r')
f2 = f.read()
print(f2)
f.close()

P.S. I am currently using Windows 11 and all my apps are updated.

In my code, I am trying to read the contents of a .txt file from my IDE. I expected the code to print the text saved in the ‘itsme.txt’ file, but it only compiles without printing anything. I also tried running the code in the console, where I received a "no such file or directory" error message.

2

Answers


  1. you have to provide full path or Absolut path if your using window

    Login or Signup to reply.
  2. If you are running in the terminal, you need to ensure that you have use command cd folder path to achieve the folder and the .txt file and .py file are in the same folder.

    Example:

    enter image description here

    enter image description here

    If you use the Run Python File provided in vscode, you need to move the .txt file to the workspace directory, usually when running the file, the terminal location stays in the workspace.

    You can also use debug mode and add the path of .txt file to the environment variable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search