Python Forum

Full Version: file open "file not found error"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello all, i am having trouble reading a simple text file, using eclipse and tried pycharm as well, both throw the same "FileNotFoundError: [Errno 2] No such file or directory:"

the file is there for sure and is located in the workspace folder of the code that i am running... its getting really annoying...
Ok, so where is the file exactly? a little more info would help resolve your problem!

Try this, then see if you can see your path and file.

import glob

# set your path here
path = '/home/pedro/summer2021/OMR/'
files = glob.glob(path + '/**', recursive=True)
for f in files:
    # shows all paths and files in path
    print(f)
You could also try

import os 
path = os.path.realpath(os.path.dirname(__file__))
print(path)
Will give the path of the folder the script executed from.
(Dec-13-2023, 09:46 AM)Pedroski55 Wrote: [ -> ]Ok, so where is the file exactly? a little more info would help resolve your problem!

Try this, then see if you can see your path and file.

import glob

# set your path here
path = '/home/pedro/summer2021/OMR/'
files = glob.glob(path + '/**', recursive=True)
for f in files:
    # shows all paths and files in path
    print(f)



see i used Path.cwd() function to get my path, then i used OPEN function to open the file. The file is in the workspace or project folder. File LOcation image <--- is the image of the file location.

Below is the code



from pathlib import Path
path1 = Path.cwd()
some_file = open(path1 / "Sample.txt")


also i tried opening a simple file using python console not via ide and i get the same result. may be there are some permissions missing or something?
(Dec-13-2023, 11:49 AM)menator01 Wrote: [ -> ]You could also try

import os 
path = os.path.realpath(os.path.dirname(__file__))
print(path)
Will give the path of the folder the script executed from.




Yes this worked fine and i got the path, i did this with Path.cwd() which works as well, the problem is only when i use any thing to open a file.
Why the spaces before and after the slashes? Are they in the path?
Here is an example that will print the lines of a text file that is,
In a folder in the current working directory, In the current working directory, and up one directory.
import os 
path = os.path.realpath(os.path.dirname(__file__))

print('\nFolder1')
with open(f'{path}/folder1/some.txt') as txt1:
    lines = txt1.readlines()
    for line in lines:
        print(line)

print('\nCurrent Working directory')
with open(f'{path}/some.txt','r') as txt2:
    lines = txt2.readlines()
    for line in lines:
        print(line)

print('\nUp one directory')
with open(f'../some.txt','r') as txt3:
    lines = txt3.readlines()
    for line in lines:
        print(line)
Just put your absolute path. That can't fail, (I think).

Quote:/path/to/myfile/myfile.txt

As jefsummers said, watch out for the spaces.
i opened a new project with pycharm, changed the directory and copied that very TEXT file to that new folder and now the same code opens it without any hesitation, what is this mystery i do not understand. Can any one explain?