Search code examples
python-3.xlistdir

Getting No file error while calling a function in python


When I am trying call an function It says no file or directory exits. But the file and directory actually exits . I have folder called XML where I have couple xml files

123.xml
456.xml
678.xml

I am try calling the function

send(uui)

It gives me the error

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-52-c4dc9477b488> in <module>
----> 1 send(uui)

<ipython-input-51-1add266355d1> in send(uui)
     11 
     12         uui = uuid.uuid4()
---> 13         mydoc = open(file)
     14         xml = mydoc.read()
     15         print(file + '_ ' + str(uui))

FileNotFoundError: [Errno 2] No such file or directory: '123.xml'

What is wrong. How to fix this?


Solution

  • os.listdir() returns the name of the file in the directory, without the directory name. Add the directory name to the file name:

    mydoc = open("XML" + os.sep + file)