3

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'
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    What does `import os` followed by `os.getcwd()` return? – ScottMcC Jul 16 '19 at 06:20
  • IDLE and VS Code don't run in the same folder... It's better anyway to open the file relative to the script https://stackoverflow.com/questions/1270951/how-to-refer-to-relative-paths-of-resources-when-working-with-a-code-repository – OneCricketeer Jul 16 '19 at 06:21
  • 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. – switchblade Jul 16 '19 at 06:26

4 Answers4

1

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.

norok2
  • 25,683
  • 4
  • 73
  • 99
  • 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. – switchblade Jul 16 '19 at 06:38
  • 1
    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). – norok2 Jul 16 '19 at 06:41
  • 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. – switchblade Jul 16 '19 at 06:47
  • 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`. – norok2 Jul 16 '19 at 06:49
  • Sure, will do so – switchblade Jul 16 '19 at 06:50
  • @switchblade, I think this should be the accepted answer – s3c Mar 30 '20 at 06:09
0

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")
  • 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")` – switchblade Jul 16 '19 at 06:31
  • Try it like this. infile=open('F:/SKKU/study/ISS3178 - Python/11/StatesANC.txt',"r") – Prasun Chakraborty Jul 16 '19 at 06:35
  • 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 Jul 16 '19 at 06:36
  • @norok2 escaping the `\ ` char fixed the issue in VS Code and it did not break in IDLE. Thanks for your help. – switchblade Jul 16 '19 at 06:41
  • @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. – norok2 Jul 16 '19 at 06:44
0

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")
ScottMcC
  • 4,094
  • 1
  • 27
  • 35
0

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/.

deon cagadoes
  • 582
  • 2
  • 13