从php启动python3脚本:字符串编码系统问题

From a php script, I want to launch a python3 script whose job is to read a file.

  • The php code looks like this :

    $command = "python3 ./toto.py" ;
    $r = shell_exec($command);
    
  • the python3 script, toto.py looks like this :

    myfile = open('/tmp/file_to_play.txt', 'r')
    print(myfile.readline())
    
  • the text file to be read is an utf-8 encoded file (note it contains a "é") :

    déclin_abeilles.mkv
    

When I launch the python3 script directly from the shell, it works properly. However, when it is executed through the php script, I get a coding system error when I read the text file :

Traceback (most recent call last):
  File "./toto.py", line 8, in <module>
    print(myfile.readline())
  File "/usr/lib/python3.4/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 32: ordinal not in range(128)

Can anybody explain me what I am missing?

Note : everything runs on a raspberry 3 server with a "Raspbian GNU/Linux 8 (jessie)" and defaults packages

Note 2 : the php code contains header('Content-type: text/html; charset=UTF-8');

From what i know, some special characters can mess up programs. Try replacing your 'é' with an 'e'. See if that helps