Oserror: File Not Found

Oserror: File Not Found

So as you can see if you search for my previous questions I'm trying to write a script that needs to rename (in the script itself, not in the folder) some .txt files to be able to use them in a loop, enumerating them.

Thanks, your answers I decided to use a dictionary, something like this:

import os
import fnmatch

dsc = {}

for filename in os.listdir('./texto'):
    if fnmatch.fnmatch(filename, 'dsc_hydra*.txt'):
        dsc[filename[:6]] = filename
        print(dsc)
print(dsc['dsc_hydra1'])

The 'print(something)' are just to check if everything is going well.

As I told in my previous questions, I need to rename them because I'm using them in future functions and I don't want to address them using all that path stuff, something like:

IFOV= gi.IFOV_generic(gmatOUTsat1,matrixINPUTsat1,dsc['dsc_hydra1'],'ifovfileMST.json',k_lim, height, width)

Using dsc['dsc_hydra1'], I get this error:

Traceback (most recent call last):
  File "mainSMART_MST.py", line 429, in <module>
    IFOV1= gi.IFOV_generic(gmatOUTsat1,matrixINPUTsat1,dsc['dsc_hydra1'],'ifovfileMST.jso',k_lim, height, width)
  File "/home/alumno/Escritorio/HDD_Nuevo/HO(PY)/src/generateIFOV.py", line 49, in IFOV_generic
    DCM11,DCM12,DCM13,DCM21,DCM22,DCM23,DCM31,DCM32,DCM33 = np.loadtxt(gmatDCM,unpack=True,skiprows = 2,dtype = float)
  File "/home/alumno/.local/lib/python3.5/site-packages/numpy/lib/npyio.py", line 962, in loadtxt
    fh = np.lib._datasource.open(fname, 'rt', encoding=encoding)
  File "/home/alumno/.local/lib/python3.5/site-packages/numpy/lib/_datasource.py", line 266, in open
    return ds.open(path, mode, encoding=encoding, newline=newline)
  File "/home/alumno/.local/lib/python3.5/site-packages/numpy/lib/_datasource.py", line 624, in open
    raise IOError("%s not found." % path)
OSError: dsc_hydra1.txt not found.

I've already checked the folder and the file is there, why I keep getting this error?

8

2 Answers

I had the same problem. In my case, inside the file.txt, I had a space at the end of the string. You should control the spaces! For example, inside the file.txt (space = -):

-365-
string1-
string2
-string3

if you remove all the spaces (-) it should work!

I had this same issue. It cannot locate the .txt file because you're in the wrong directory. Make sure that where you're trying to execute the code is within the directories of which the code needs. Hope this helps.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor
Author

Sophia Al-Mansoor

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.