
fredebec
France
Asked
— Edited

Hi,
I try to convert an old EZ scripts into a python scripts It is a script that reads a number from a txt file, makes a calculation, then rewrite the file with the new value.
The following EZ scripts works fine:
# Close the text file to make it accessible
FileReadClose("C:\Users\FredWin\Documents\ARC\Scripts\Calc.txt")
# Obtain a number from the text files and use it as variable
$calc = FileReadAll("C:\Users\FredWin\Documents\ARC\Scripts\Calc.txt")
# Show the number in the debug window<br />Print("old value:")
Print($calc)
# Calculation
$calc = $calc + 1
Print("---")
Print("new value:")
Print($calc)
FileDelete("C:\Users\FredWin\Documents\ARC\Scripts\Calc.txt")
FileWrite("C:\Users\FredWin\Documents\ARC\Scripts\Calc.txt", $calc)
But the following python script does not:
# Close the text file to make it accessible
File.ReadClose("C:\Users\FredWin\Documents\ARC\Scripts\Calc2.txt")
# Obtain a number from the text files and use it as variable
setVar("$calc2", File.ReadAllText("C:\Users\FredWin\Documents\ARC\Scripts\Calc2.txt") )
calc2 = (getVar( "$calc2" ))
# Show the number in the debug window
print("old value")
print(calc2, type(calc2))
# Calculation
calc2 = ( calc2 + 1)
print("---")
print("new value")
print(calc2, type(calc2))
File.Delete("C:\Users\FredWin\Documents\ARC\Scripts\Calc2.txt")
File.Append("C:\Users\FredWin\Documents\ARC\Scripts\Calc2.txt", calc2)
The problem seems to be with the "File.Append" function. Instead of putting a number for the new calc2 value in the txt file, it writes a special character. So after that, calc2 is considered as a string, and the scripts cannot make the calculation anymore.
Any idea ? Thanks
Related Hardware EZ-B v4
Related Control
Script
Also note the filename from PTP's solution. You cannot use a single backslash "" as this is used to denote a special character. Most python variations will allow you to use the forward slash "/" instead without any problems ie
'c:/folder/subfolder/filename'
Another option is :-
import os myfile = os.path.normpath('c:/folder/subfolder/filename') open(myfile)
I'll have to test how these work with ARC python.
skidroe
Using the forward slash it will work on Windows and Linux. Tested with ARC and this works fine :-
myfile = "C:/temp/Calc.txt"
f = open(myfile) val = int(f.read()) f.close()
print "Old Value is ", val val = val + 1 setVar("$calc", val)
print "New Value is ", val
f = open(myfile,"w") f.write(str(val)) f.close()
Thank you all for your help and all the information. Some is a little beyond my actual knowledge, but I will learn... :-)
I have to admit I am a little lost between what is specific to Python and what is related to ARC. For example, to open and write into a file, I don't know if I should use "File.Read..." and "File.Append..." or some command suggested by @skidroe ("open(myfile)", "...write",...) and what's the difference...
JS:
Python
So you can see there are consistency between languages and features.
Returning to your Python code: File.* is a ARC object not a native Python2 object.
@Mickey script post #2:
or @skidroe script post #10
They use Python 2, and btw Python3 file APIs are slightly different, but, you need to stick with Python 2. The exception is the ARC: setVar function.
ARC Javascript uses a .NET JS interpreter https://github.com/sebastienros/jint and has Full support for ECMAScript 5.1: http://www.ecma-international.org/ecma-262/5.1/ The component only supports the language so File access requires the ARC File object.
ARC Python uses IronPython a .net dynamic language, and supports Python 2 standard lib which includes different modules like file access, networking, serial etc. So if you want to access the file system, or even the .NET classes you can do it from Python, you don't need to use File ARC object.
other objects like: Audio, ADC, Ping etc, are specific to ARC features. IronPython detailed documentation: https://ironpython-test.readthedocs.io/en/latest/contents.html
@ptp, thanks a lot for your explanations. It is really helpful !
I am now going to play a little bit with everything I have learned in this thread...
Hey guys, I also learned a lot here...I did not really pay attention how Python is being implemented within ARC, and just used Python 3 out of habit! Ups!!