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


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


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
  Error (Errno 2), File upload with the Flask framework and a public IP Username_Python1 0 281 Mar-28-2024, 01:46 PM
Last Post: Username_Python1
  Absolute paths in subprocess - file not found kittyticker 4 500 Jan-28-2024, 10:37 PM
Last Post: kittyticker
  Open/save file on Android frohr 0 337 Jan-24-2024, 06:28 PM
Last Post: frohr
  error "cannot identify image file" part way through running hatflyer 0 681 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  Need to replace a string with a file (HTML file) tester_V 1 776 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can i combine these two functions so i only open the file once? cubangt 4 875 Aug-14-2023, 05:04 PM
Last Post: snippsat
  How can I change the uuid name of a file to his original file? MaddoxMB 2 944 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Coding error. Can't open directory EddieG 6 1,137 Jul-13-2023, 06:47 PM
Last Post: deanhystad
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,394 Jun-27-2023, 01:17 PM
Last Post: diver999
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,116 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone

Forum Jump:

User Panel Messages

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