0

My program is located under C:\Users\Username\pycode.py

My file to be called within the program is located under C:\Users\Username\<filename>

The file and the code are in the same directory.

When I call from within the program using the below set of codes,

import os; import sys
with open(os.path.join(sys.path[0], "file1.txt"), "r") as fn1:
    print(fn1.read())

the Py code picks up the file and executes the applied methods on it. However, when I don't use the above code and use as:

fn1 = open("file1.txt", "r")
print(fn1.read())

There is only an error that the file does not exist.

Any thoughts and ideas, what is missing to make the file within the local directory as the py code to recognize the file.

If it's relevant, I'm a beginner using VSCode on Windows 10.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Stan
  • 33
  • 8
  • 1
    Related: [What exactly is current working directory?](/q/45591428/4518341) – wjandrea Nov 07 '21 at 16:48
  • What is `sys.path[0]`? – wjandrea Nov 07 '21 at 16:55
  • As mentioned in the previous comments, 1. The filename is being called correctly. 2. The issue is that the file which is called and the python code file are within the same directory, which should not require to the absolute path of the file to be used. – Stan Nov 07 '21 at 16:56
  • Maybe ultimately you want to use the `__file__` variable? Usage is covered in [How do I get the full path of the current file's directory?](/q/3430372/4518341) – wjandrea Nov 07 '21 at 17:00

1 Answers1

0

Speaking from personal experience, it happens to me when I am using vscode and I am in a directory say d:/Desktop/python programs. But the file I want to open() is in the directory d:/Desktop/python programs/fun.

In this case it is not sufficient for me to use the path like "filename". Instead I need to use "/fun/filename". This is even though the file I am working on is in the fun folder.

So perhaps you could go to the terminal in VSCode (your IDE) and run cd "path to your file" and that should solve the problem

Try running the following code and see what output you get

import os
print(os.getcwd())

In my case this would print d:/Desktop/python programs

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Muhd Mairaj
  • 557
  • 4
  • 13
  • Yes, as stated earlier, when using the Absolute path of the file, it does work fine. The issue is when the VSCode is being used. Thanks for the update. However, I am looking forward to the actual solution to this issue. Any assistance is much appreciated. – Stan Nov 07 '21 at 18:02
  • @Stan did you try to change directory in the terminal as i mentioned in my answer? The solution is to use the absolute path, or to change your working directory to the correct folder. You could also use a relative path that is relative to the current working directory. What type of solution are you looking for? – Muhd Mairaj Nov 07 '21 at 18:06
  • @Muhd.Mairaj Your solution worked. Thanks. I did try changing the path of the terminal to the path in which the called file and the python program were both located and the program executed successfully. Your sharp insight is much appreciated. Thanks – Stan Nov 07 '21 at 18:12