Search code examples
python-3.xreadfile

FileNotFoundError even though file exist


when trying to open a file using wit open .. getting error that file doesn't exist.

I am trying to parse some txt files , when working localy it works with no issue, but the issue started when I am trying to connect to a network folder. the strange this is that is does see the file , but says its not found . The Path I referring is '//10.8.4.49/Projects/QASA_BR_TCL_Env_7.2.250/Utils/BR_Env/Call Generator/results/Console_Logs/*' (this folder is full of txt files.

but I am still getting this error: FileNotFoundError: [Errno 2] No such file or directory: 'Console_log_01-01-2019_08-17-56.txt'

as you see , it does see the needed file . in order to get to this file I am parsing splitting the path the follwoing way :

readFile = name.split("/")[9].split("\\")[1]

because if I am looking on the list of my files I see them the following way :

['//10.8.4.49/Projects/QASA_BR_TCL_Env_7.2.250/Utils/BR_Env/Call Generator/results/Console_Logs\Console_log_01-01-2019_08-17-56.txt',

after splitting I am getting :

Console_log_01-01-2019_08-17-56.txt

and still it says the file is not found.

def main():
        lines =0
        path = '//10.8.4.49/Projects/QASA_BR_TCL_Env_7.2.250/Utils/BR_Env/Call Generator/results/Console_Logs/*'
        files = glob.glob(path)
        print ("files")
        print ('\n')
        print(files)
        for name in glob.glob(path):
            print (path)
            readFile = name.split("/")[9].split("\\")[1]
            print(readFile)
            with open(readFile,"r") as file:
                lines = file.readlines()
                print (lines)
main()

files
['//10.8.4.49/Projects/QASA_BR_TCL_Env_7.2.250/Utils/BR_Env/Call Generator/results/Console_Logs\\Console_log_01-01-2019_08-17-56.txt', '//10.8.4.49/Projects/QASA_BR_TCL_Env_7.2.250/Utils/BR_Env/Call Generator/results/Console_Logs\\Console_log_01-01-2019_08-18-29.txt']

Traceback (most recent call last):
//10.8.4.49/Projects/QASA_BR_TCL_Env_7.2.250/Utils/BR_Env/Call Generator/results/Console_Logs/*
Console_log_01-01-2019_08-17-56.txt
  File "C:/Users/markp/.PyCharmEdu2018.3/config/scratches/scratch_3.py", line 19, in <module>
    main()
  File "C:/Users/markp/.PyCharmEdu2018.3/config/scratches/scratch_3.py", line 16, in main
    with open(readFile,"r") as file:
FileNotFoundError: [Errno 2] No such file or directory: 'Console_log_01-01-2019_08-17-56.txt'

Process finished with exit code 1

Solution

  • When you are looking for the file you are looking in the entire path, however when you are opening the file, you are referencing it as if it was in the local path, either change the current working directory with

    os.chdir(path)
    

    before opening the file, or in the open statement use

    open(os.path.join(path,filename))
    

    I recommend the first approach if you have to open only one file in your program and second if you open multiple files at multiple directories.

    In future better format your questions, stack overflow has multiple tools, use them, also you can see how your text looks, make sure to have a look at it before posting. Use the code brackets for your code, that will help whoever is trying to answer.