Forums

Scheduled tasks : files not found

Hello everyone,

My schedules scripts failed to launch because required files are not found. Please what is the path of the main repository?

The complete traceback is shown below

Traceback (most recent call last):

File "botjagwar/dikantenyvaovao.py", line 9, in <module>

fiteny = eval(file('addtrad.dct','r').read())

IOError: [Errno 2] No such file or directory: 'addtrad.dct'

the file addtrad.dct is in the same folder as the file dikantenyvaovao.py.

Hi radotranonkala,

This should be resolved in a few minutes. It is not a problem with your script but the underlying system.

Hi hansel,

I have tried at 12.00 pm but it still doesn't work. Is the problem fixed now?

Yes it should have been fixed awhile ago. What is the exception you are seeing in your logfile?

I'm still having an IOError like the one above. It seems that using relative paths in scheduled scripts doesn't work... Any explanations on this limitation?

It's just how the operating system works. All file paths are relative to the current working directory. You can inspect what that is in your script by doing:

import os
os.getcwd()

That will tell you what the current working directory is and you can build you paths from that. Or just use absolute paths.

But I'm still a bit concerned that this might be a hangover from the earlier outage. Does providing an absolute path fix the problem?

To answer your question, yes, it does. The work will be tedious if I decide to use absolute paths everywhere.

I've inspected the current working directory (c.w.d.) by doing what you wrote above. Assuming the python 2.6 console behaves exactly like the interpreter in the Scheduler, it is not the c.w.d. where the script is, but rather the main file repository i.e /home/radotranonkala/, hence the IO Errors.

Right now I'll try to configure by using os.chdir(path).

Typically to make an application portable you would use

os.path.abspath(os.path.dirname(__file__))

That gives you the absolute path of the file you are currently working in. Then you can join on your subdirectories. I agree it would be tedious to hardcode absolute paths everywhere.

You can use os.chdir(), but it's still a somewhat fragile approach - it's generally good practice to use absolute paths. For example, if you end up calling other code which changes your current working directory (which would be a bit of a naughty thing to do, but you can't rely on third party code to be perfect) then you'll end up with some subtle bugs that may be hard to track down.

Using absolute paths everywhere is generally not too tedious if you can pick a base directory that everything else can be relative to. Then your code ends up looking like:

with open(os.path.join(BASEDIR, "dir1", "dir2", "file.ext")) as fd:
    # ... do stuff

That's not too tedious, is it?

One choice of base directory is to use the __file__ of your source file as Hansel suggests. This is fine if your data file is always in the same place, but if you ever decide to distribute your script then it'll probably end up in a different location. There are other approaches, but it depends what you need.

Hi and thanks to Cartroo and hansel for their advices.

As my datas always stay in the same place, I'll try the solution proposed by hansel; but I'll remember your original solution (actually I've never proceeded like you did above).

I was looking for an emergency solution to the IO Errors, so I used os.chdir as it seemed to be the easiest way to fix the problem.

Best regards,