Python Forum
file open "file not found error"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
file open "file not found error"
#1
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...
Reply
#2
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)
Reply
#3
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.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#4
(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?
Reply
#5
(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.
Reply
#6
Why the spaces before and after the slashes? Are they in the path?
Reply
#7
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)
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#8
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.
Reply
#9
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  module not found error Pedroski55 15 177 Jun-10-2026, 05:41 PM
Last Post: noisefloor
  Control of the number of daemon error messages to the file MarPan 3 86 Mar-25-2026, 11:00 AM
Last Post: DeaD_EyE
  open a text file using list() Pedroski55 2 114 Feb-25-2026, 06:57 PM
Last Post: noisefloor
  If I open a file write or append, is the file loaded into RAM? Pedroski55 11 1,118 Jan-14-2026, 07:49 AM
Last Post: Pedroski55
Question [SOLVED] Open file, and insert space in string? Winfried 7 2,523 May-28-2025, 07:56 AM
Last Post: Winfried
  How to write variable in a python file then import it in another python file? tatahuft 4 2,253 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Trying to open depracated joblib file mckennamason 0 2,156 Sep-19-2024, 03:30 PM
Last Post: mckennamason
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 2,250 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 2,846 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  Module not found error even though installed NZGeorge 1 8,179 Jul-10-2024, 09:08 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020