Check existing file

Hello !!
This thread follows a previous one, located here :
http://forum.anyscript.org/showthread.php?t=3169

In brief, I’d like to check if a file exists in a folder before creating an output file. Would you have any advice about thaat ?

Thank you for your help !
Lauranne

Lauranne,

we have a short test example that does exactly that. However, it uses python to do so. Are you familiar with the use of AnyBody and Python?

Amir,
Thanks a lot for your help. I absolutely don’t know the Python language but your example was clear enough to integrate it into my model.

I"m not sure, but i think there is a little mistake in your “filename.py” function : you specify a “context” but never defined it in the AnyBody code (line : def name(context, Basepath, Basename, ext)). Removing this parameter made the function working.

Thanks again.
Lauranne

Hi Lauranne,

that sounds a bit suspicious to me because for me it is the other way around: when I remove the context variable from the python script the model does not load, and I tested it from the AMS versions 5.2.1 and newer. Anyway, if it is working for you like that leave it.
The context variable is defined by the AnyFunExMonoPy and delivers some information from about the interface like the calling .any script, the filename of the .py script and some other things which you can see in the class example of the reference manual.

Best regards
Daniel

Daniel,
If I understand well the code, adding “context” in the python file means that it would also be described in the anybody code when referring the function. In other word, it should be defined in the ArgList, what is not the case :

AnyFunEx get_filename =
    {
      AnyString Return = "";  
      //AnyVar Return = 0.0;
      AnyFunExMonoPy name = 
      {
        ModuleFile = "filename.py";
        
        ArgList = 
        {
          AnyString Basepath = {""};
          AnyString Basename = {""};
          AnyString ext = {""};
        };
      };
    };

What kind of information should be included in the “context” parameter ?

Best regards,
Lauranne

Lauranne,

that is almost true. You can think of the parameter context as predefined and used from the AnyFunEx class which calls and executes the python function, so the values in this context variable can be used in the python script but there is no change to change the these values from AnyScript.

As far as I know the two script names mentioned in the last post and name and abolute path of the AnyScript functions are in this variable. I don’t know what else is in there, but to find out I can recommend to play around with class example, this is a very small example so it should be quite easy to modify it even without knowing much about Python.

May I ask which AMS version you are using currently?

Best regards
Daniel

Daniel,
after many tests :eek: , I finally obtained what I wanted :slight_smile: Thanks again for your advices !

Regarding the context parameter : in fact it doesn’t matter if it is specified or not in the “.py” file.

My AMS version is the 5.1.0.

Best regards
Lauranne