runShellCommand fails with python script and f=open(file)

Hello, I'm using the runShellCommand() to execute a python script.
I've used this function with simple scripts like the following, and it works well.

hello_world.py

print "Hello World"

I can receive this string on the Arduino side.

But when I try to use the following script it returns nothing.

read_file.py

f = open ('text.txt','r+')
print f.readline()

I've executed this script on the console and it works well, it reads the first line of the text.txt and prints it.

I've noticed this problem happens when I call the open() function, when I don't call it Arduino executes correctly the script. What is doing this open() function that is not working when the script is called from Arduino?

Arduino sketch:

read_file.ino

#include <Process.h>

void setup() {
  // Initialize Bridge
  delay(60000);
  Bridge.begin();

  // Initialize Serial
  Serial.begin(9600);

  // Wait until a Serial Monitor is connected.
  while (!Serial);
  runPython(); // call python
}

void loop() {
 //not used
}

//our Python callback
void runPython() {
  
  Process p;        // Create a process and call it "p"
  p.runShellCommand("python /mnt/sda2/scripts/read_file.py");  // use CLI commands just like 
                                                     //inside linux console
  // A process output can be read with the stream methods
  while (p.available()>0) {
    char c = p.read();
    Serial.print(c);
  }
  // Ensure the last bit of data is sent.
  Serial.flush();
}

mart256:
::::SNIP::::

I've noticed this problem happens when I call the open() function, when I don't call it Arduino executes correctly the script. What is doing this open() function that is not working when the script is called from Arduino?

::::SNIP::::

@mart256,
I have not tested this, nor your code, nor your idea. So this is a stab in the dark.

You will need the give the FQFN (Fully Qualified File Name) to python. Just like you are using the FQFN to call the python script from your sketch, you need to do the same in python when opening the file.

So read_file.py should be:

f = open ('/path/to/file/text.txt','r+')
print f.readline()

I'll check your idea later tonight.

Jesse

Yes, I'm virtually positive that it is a relative pathname problem. You are opening the file test.txt for read/append access, but where is the file located? When you are running it from the command line, the script's current directory is the directory you were in when you issued the command. But when running from a Process object, the script will be running from a different folder (not the folder that contains the script.)

Jesse nailed the answer: use the fully qualified file name in the Python code.

Thanks a lot jesse and Shape, that was indeed the problem. I used the complete path and now it works!

mart256:
Thanks a lot jesse and Shape, that was indeed the problem. I used the complete path and now it works!

I'm very happy to hear it's working for you.
:slight_smile: :smiley: 8)

Jesse