I have two Python-Sctipts. The first one is to start CoDeSys and commanding it to execute a second Python-Script. The second script is to open one CoDeSys-Project and to exporting it to XML. The first script: ```
My script should work on all computers without changing the directory, thats why i changed the first script. I have now two functions; the first one searchs for CoDeSys.exe and the second for my second script:
def runProcess(CoDeSys):
  for root, dirs, files in os.walk('C:\'):
    if CoDeSys in files:
      return (os.path.abspath(os.path.join(root, CoDeSys)))
def PythonScript(ExportScript):
  for root, dirs, files in os.walk('D:\'):
    if ExportScript in files:
      os.system((os.path.abspath(os.path.join(root, ExportScript))))
```Problem: how can i tell CoDeSys to start the second script from inside of CoDeSys??
I hope my problem is clear
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2012-01-17
Originally created by: M.Schaber
Hi, aoj,
I understand what you try to achieve, but I don't understand what your problem is, sorry.
You've got the call to start codesys, and you've got methods to find the path of the script and CoDeSys.exe itsself, so everything seems to be there...
Markus
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Markus you are my hero
I didnt know how to build the CoDeSys command line using the subprocess-Modul. Now it is working. Thank you again and wish you a nice day.
AOJ
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Btw, your methods of searching the CoDeSys.exe is not fail-proof: If several CoDeSys versions are installed in different directories in "C:\Program Files", your script will match the first CoDeSys.exe it can find, and this is not necessarily the one matching the profile you give.
HTH,
Markus
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
What should I change to make fail-proof? And for the case if I dont know in which directory is saved, is it possible to run it without giving the directory in the method?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2012-01-18
Originally created by: M.Schaber
Hi, aoj,
aoj hat geschrieben:
What should I change to make fail-proof? And for the case if I dont know in which directory is saved, is it possible to run it without giving the directory in the method?
AFAICS, there's no perfect approach for this, as there can be several CoDeSys version installed in parallel, and the script cannot guess which one you wanted to start.
I know the following approaches which all are being used in the field:
Note that the directory containing a script is usually contained in the search path. Additionally, in most cases, a python script or module can detect its own directory using a construct like the following:
```As you can see the script exports the object Device. What about if I have in my project 50 objects to export? Is it possible to export the whole project?
Thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2012-01-18
Originally created by: M.Schaber
Hi, aoj,
The Project itsself also has an export_native method, which allows to pass a list of objects to export, as well as an recursive parameter to include child objects.
Simply exporting the whole project could be done with a command like:
Hi,
I have two Python-Sctipts. The first one is to start CoDeSys and commanding it to execute a second Python-Script. The second script is to open one CoDeSys-Project and to exporting it to XML. The first script: ```
subprocess.call(r'"C:\Program Files (x86)\3S CoDeSys\CoDeSys\Common\CoDeSys.exe" --Profile="CoDeSys V3.4 SP4 Patch 1" --runscript="D:-----Python32\Export_New.py"')
My script should work on all computers without changing the directory, thats why i changed the first script. I have now two functions; the first one searchs for CoDeSys.exe and the second for my second script:
def runProcess(CoDeSys):
  for root, dirs, files in os.walk('C:\'):
    if CoDeSys in files:
      return (os.path.abspath(os.path.join(root, CoDeSys)))
def PythonScript(ExportScript):
  for root, dirs, files in os.walk('D:\'):
    if ExportScript in files:
      os.system((os.path.abspath(os.path.join(root, ExportScript))))
```Problem: how can i tell CoDeSys to start the second script from inside of CoDeSys??
I hope my problem is clear
Originally created by: M.Schaber
Hi, aoj,
I understand what you try to achieve, but I don't understand what your problem is, sorry.
You've got the call to start codesys, and you've got methods to find the path of the script and CoDeSys.exe itsself, so everything seems to be there...
Markus
Hi M.Schaber,
I am trying to call the two functions with the following code:```
codesys = "CoDeSys.exe"
subprocess.call(runProcess(r"%s" % codesys))
exportscript = "Export.py"
os.system(PythonScript(exportscript))
```
The project doesnt get exported because the second script deosnt get run from inside of the CoDeSys environment. Is it now more clear?
Originally created by: M.Schaber
Hi,
In your first post, you had the following code:
This code shows how the command line to start CoDeSys has to be built together. Then, you should build the CoDeSys command line using this pattern.
Why do you to call CoDeSys without arguments, and afterwards try to start the script outside of CoDeSys with os.system instead?
Try something like the following:
HTH,
Markus
Markus you are my hero
I didnt know how to build the CoDeSys command line using the subprocess-Modul. Now it is working. Thank you again and wish you a nice day.
AOJ
I have one more question Is it possible to run CoDeSys without giving the directory of CoDeSys.exe in my method?
Originally created by: M.Schaber
Hi, AOJ,
The building of the command line is not related to the subprocess-module, it is just a normal string operation. You could do the following:
The % operator for string formatting is documented in the python documentation: http://docs.python.org/library/stdtypes.html#string-formatting-operations
Another way would be to use string concatenation:
Btw, your methods of searching the CoDeSys.exe is not fail-proof: If several CoDeSys versions are installed in different directories in "C:\Program Files", your script will match the first CoDeSys.exe it can find, and this is not necessarily the one matching the profile you give.
HTH,
Markus
What should I change to make fail-proof? And for the case if I dont know in which directory is saved, is it possible to run it without giving the directory in the method?
Originally created by: M.Schaber
Hi, aoj,
AFAICS, there's no perfect approach for this, as there can be several CoDeSys version installed in parallel, and the script cannot guess which one you wanted to start.
I know the following approaches which all are being used in the field:
Note that the directory containing a script is usually contained in the search path. Additionally, in most cases, a python script or module can detect its own directory using a construct like the following:
You can use this to find the directory of the Export_New.py script if it is installed in the same directory (or relative path) to the first script.
HTH,
Markus
Hi Markus,
thanks a lot for your help.
One more question
The following code is a part of my ExportScript:```
proj = projects.open(pfad)
device = proj.find('Device', recursive = False)[0]
device.export_native(destination=utils.temp(File), recursive = True)
print ("exporting finished")
```As you can see the script exports the object Device. What about if I have in my project 50 objects to export? Is it possible to export the whole project?
Thanks
Originally created by: M.Schaber
Hi, aoj,
The Project itsself also has an export_native method, which allows to pass a list of objects to export, as well as an recursive parameter to include child objects.
Simply exporting the whole project could be done with a command like:
That is what i was searching for. Endless THANKS