skip to Main Content

I have been trying to open a file from Visual Studio Code for a task, but it doesn’t open. It always says "errno 2, no such file". I did open it once copying the path from the terminal, but it’s not working any more. I have tried many things that I have seen on Google Search or YouTube, but none have worked.

Right now I’ve been taught to open the file with this method:

file = open('input.txt', 'r+')
print(file)

Before I opened another file like this:

file = open('/Users/MYFULLNAME/Desktop/DOB.txt', 'r+')
print(file)

But this method isn’t working any more. How can I fix it?

2

Answers


  1. It seems like you’re encountering an issue with opening a file using Visual Studio Code. The error message "errno 2, no such file" usually indicates that the file you’re trying to open doesn’t exist in the specified location.

    Here are a few suggestions to help you troubleshoot and resolve the issue:

    • Check the file path: Make sure the file you want to open (‘input.txt’) is located in the same directory as your Python script or provide the correct path to the file. You can use the absolute file path to ensure accuracy.

    • Verify the file name and extension: Ensure that the file name is spelled correctly, including the correct capitalization if necessary. Also, double-check that the file extension is correct (‘.txt’ in this case).

    • Confirm file permissions: Ensure that you have the necessary permissions to access and modify the file. Make sure the file isn’t open in another program that might restrict access.

    • Use the full file path: Instead of relying on a relative file path like ‘input.txt’, try using the full file path to ensure accuracy. For example:

    import os
        
    file_path = 'input.txt'
        
    if os.path.exists(file_path):
        file = open(file_path, 'r+')
        print(file)
    else:
        print('File does not exist.')
    

    This code will print "File does not exist." if the file doesn’t exist or the full file object if it does exist.

    Try these steps and let me know if you’re still experiencing difficulties.

    Login or Signup to reply.
  2. It seems the issue is not with Visual Studio Code, but rather with the path that you are defining to open the text file.

    I’m assuming (from your second code snippet) your OS is Windows. You need to go to the location where you kept the text file. Right click on the file and select "Copy as path". This will copy the exact location of the file including its name and extension.

    Once copied, you can paste the path to the following code:

    with open("/path/you/copied", 'r') as file:  # make sure to replace '' with '/' in the path
        print(file.readlines()) # this will print out all the lines in your text file in a list
        lines = file.readlines() # this will store all the lines in your text file as a list
        file.close() # this will ensure the text file in your HDD does not get corrupted
    

    Now if you want to access each line from your text file, you can simply call the index in the list you created earlier by the following:

    line_1 = lines[0]
    line_2 = lines[1]
    

    You can also get a bit more detailed explanations here.

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