简体   繁体   English

subprocess.run 返回 ValueError:嵌入式 null 字节

[英]subprocess.run returns ValueError: embedded null byte

Explanation解释

I try to start a.sh file inside a python3 script with subprocess.run data is the content of a RFID Card with 16 3-digit numbers.我尝试使用 subprocess.run 数据在 python3 脚本中启动 a.sh 文件,数据是具有 16 个 3 位数字的 RFID 卡的内容。 I write a Filename to the card-content which is then converted to ASCII format.我将文件名写入卡片内容,然后将其转换为 ASCII 格式。 When reading the card it is converted back to the Filename.读取卡时,它会转换回文件名。

Code代码

# Check if authenticated
if status == MIFAREReader.MI_OK:
    data = MIFAREReader.MFRC522_Read(8)
    # delete blank spaces
    res = [0 if item == 255 else item for item in data]
    # data is an ASCII code from an RFID card, 
    foldername = "".join(chr(x) for x in res)
    videofile = (str(foldername)+".sh")
    # check if videofile has the correct value/name
    print(videodatei)
    videostart = ("/home/pi/Documents/" +videodatei)
    # check if the complete folder has the correct value
    print(videostart)
    #it is working and returns the exact same value as the working code below
    #this one is working: subprocess.run(['/home/pi/Documents/jonah_aktuell.sh'])

Exception例外

subprocess.run([videostart])

returns the following error:返回以下错误:

Traceback (most recent call last): File "MyRead3.py", line 70, in subprocess.run([videostart]) File "/usr/lib/python3.7/subprocess.py", line 472, in run with Popen(*popenargs, **kwargs) as process: File "/usr/lib/python3.7/subprocess.py", line 775, in init restore_signals, start_new_session) File "/usr/lib/python3.7/subprocess.py", line 1453, in _execute_child restore_signals, start_new_session, preexec_fn) ValueError: embedded null byte回溯(最后一次调用):文件“MyRead3.py”,第 70 行,在 subprocess.run([videostart]) 文件“/usr/lib/python3.7/subprocess.py”,第 472 行,与 Popen 一起运行(*popenargs, **kwargs) 作为进程:文件“/usr/lib/python3.7/subprocess.py”,第 775 行,在init restore_signals,start_new_session 中)文件“/usr/lib/python3.7/subprocess.py ",第 1453 行,在 _execute_child restore_signals、start_new_session、preexec_fn) ValueError:嵌入式 null 字节

What I have tried我试过的

As you can see "videostart" and it's content don't contain a NULL.正如您所看到的“videostart”,它的内容不包含 NULL。 I also checked, that the variable is a string and made sure with the string() function.我还检查了该变量是一个字符串,并确保使用 string() function。

I googled tons of "solutions", but all of the problems contained NULLs somewhere in the variable content, mine doesn't.我用谷歌搜索了大量的“解决方案”,但所有问题都在变量内容的某处包含 NULL,我的没有。

I also tried subprocess.call() with the same result.我也尝试了 subprocess.call() 具有相同的结果。

  • chr(0) is a NUL byte. chr(0)是一个 NUL 字节。
  • UNIX syscalls have their arguments passed as C strings. UNIX 系统调用的 arguments 作为 C 字符串传递。
  • C strings are NUL-terminated -- meaning, they're considered to end at the first NUL byte. C 字符串以 NUL 结尾——意思是,它们被认为在第一个 NUL 字节处结束。

Thus, you cannot pass chr(0) as part of a command-line argument on UNIX, so res = [0 if item == 255 else item for item in data] ensures that foldername = "".join(chr(x) for x in res) will generate a value that cannot be passed in an argument vector if any value contains either 0 or 255.因此,您不能将chr(0)作为 UNIX 上的命令行参数的一部分传递,因此res = [0 if item == 255 else item for item in data]确保foldername = "".join(chr(x) for x in res)如果任何值包含 0 或 255,则foldername = "".join(chr(x) for x in res)将生成一个无法在参数向量中传递的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM