skip to Main Content

Recently got a new laptop, with that needed to transfer over my programs and projects. I made and then cloned a private repository on GitHub onto my new laptop, however anytime I run a program that needs to access any .txt files I get the same error. The .txt files are in the same folder as the program and it runs on my old laptop completely fine. Also any program that does not need to search for any external files works just fine. Provided a picture that shows the folder and the error given. folder
error

Tried reinstalling VSCode, did not fix it. I assume I made a mistake with the the git repository or something. Appears to be a path issue of some sorts but was unable to find a solution online.

Edit: Relatively new to programming, also wanting to avoid hardcoding the absolute path as I am in school and my program needs to run on the markers computer.

3

Answers


  1. You can note that the current folder in terminal is the worksapce instead of the folder where the file you ran was.

    You can use the following codes to open:

    with open('Lab 3klingon-english.txt') as file:
    

    You can also add the path of klingon-english.txt to environment variable.

    enter image description here

    Login or Signup to reply.
  2. Try this. Right click on your text file which you want to open and copy path as shown. Now paste this path in your openfunction on python file where you have.

    enter image description here

    Login or Signup to reply.
  3. to avoid hard coding the full path, you can use os.getcwd() like this:

    import os
        
    text_file = f"{os.getcwd()}\subfolder\file.txt"
    

    The function os.getcwd() gets you a string of the current working directory.

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