4

So I am writing a code where I need to import a file. Both the python code file and the file I need to import are in the same directory thus I haven't specified the whole path.

The code works fine in IDLE but I get an error in Visual Studio Code

I added the line "print(few)" to check if it worked in IDLE. It does print it.

import random
infile=open("StatesANC.txt","r")
print("few")

The error I get in Visual Studio is as follows:-

Traceback (most recent call last):
File "f:/SKKU/study/ISS3178 - Python/11/Lab Assignment 11.py", line 2, in <module>
infile=open("StatesANC.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'StatesANC.txt'
3
  • 1
    What does import os followed by os.getcwd() return? Commented Jul 16, 2019 at 6:20
  • 1
    IDLE and VS Code don't run in the same folder... It's better anyway to open the file relative to the script stackoverflow.com/questions/1270951/… Commented Jul 16, 2019 at 6:21
  • 1
    I did not really understand where did you want me to put that that code and how to follow it up to the second part as I am quite new to python. So here's what the code looks like after making changes to it based on your suggestion- import os os.getcwd() import random infile=open("StatesANC.txt","r") print("few") There was no change in the output from IDLE and no change in the error from VS Code. Commented Jul 16, 2019 at 6:26

4 Answers 4

2

As others have pointed out, the problem is that the default working directory for IDLE and Visual Studio Code may be different.

One way to test this is to include the following at the beginning of your script:

import os
cwd = os.getcwd()
print(cwd)

If that is actually the case, you could change your working directory to the one of your script (whose file path is stored in the special variable __file__) with:

import os

script_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(script_path)

This code should be put before opening the file.

Sign up to request clarification or add additional context in comments.

6 Comments

I included the first set of code given by you into mine but still, get the same error in VS Code. But in IDLE I get the following output - F:\SKKU\study\ISS3178 - Python\11 Also, I do not understand what you said in the second part of your reply.
The first part is to check that your script runs in different working paths, depending on whether it is run in VS Code or IDLE. You have to put this BEFORE you open the file (perhaps you should comment that part from your script temporarily. The second part is actually a way to fix it: it is basically getting the path of your script and changing your working directory to that so that when open(... is issued, it should find the file correctly (even if you later move the script and the file to a different directory).
So do I just copy paste the second part or I have to make changes to it like add the path? I fell like I am supposed to make changes but I am not sure.
No need to make changes, just have that code on top of your script, but perhaps also try to understand it ;-) I would advise to add a few print statement to see what is in __file__, os.path.abspath(__file__) and script_path.
Sure, will do so
|
1

Give the full path and it should run from everywhere

say you are using linux system and your file is in xyz folder in home

import random
infile=open("/home/xyz/StatesANC.txt","r")
print("few")

5 Comments

If I type in the whole path I get the following error in both IDLE and VS Code OSError: [Errno 22] Invalid argument: 'F:\\SKKU\\study\\ISS3178 - Python\t\\StatesANC.txt' The path I put in is - infile=open("F:\SKKU\study\ISS3178 - Python\11\StatesANC.txt","r")
Try it like this. infile=open('F:/SKKU/study/ISS3178 - Python/11/StatesANC.txt',"r")
When using the \ char inside a string you should either escape it (i.e. substitute it with \\ or use the r (raw) string qualifier: r"C:\SKKU..."
@norok2 escaping the \ char fixed the issue in VS Code and it did not break in IDLE. Thanks for your help.
@switchblade beware though, that this solution is quite brittle since moving your txt file (or renaming any directory along its path) will break your script, check my answer for a more robust approach.
1

Assuming the text file is in the same directory as your python file you should be able to do the following:

import random
import os

file_name = "StatesANC.txt"
basedir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(basedir, file_name)

infile=open(file_path,"r")
print("few")

Comments

1

Put absolute path on open(r"/rootpath/subpath/StatesANC.txt","r") probably the file StatesANC.txt isn't on path f:/SKKU/study/ISS3178 - Python/11/.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.