如何在 Linux 上使用 Python 获取文件夹组?

问题描述

我想获得一组目录。我尝试了这个 question 的答案,但都不适合我。

如果我通过终端列出文件,我会得到正确的组 gpio。

$ ls -l /sys/class/gpio/
total 0
--w--w---- 1 root gpio 4096 Feb 26 15:07 export
lrwxrwxrwx 1 root gpio    0 Feb 26 15:07 gpio248 -> ../../devices/6000d000.gpio/gpiochip0/gpio/gpio248
lrwxrwxrwx 1 root gpio    0 Feb 26 14:19 gpio250 -> ../../devices/6000d000.gpio/gpiochip0/gpio/gpio250
lrwxrwxrwx 1 root gpio    0 Feb 26 14:35 gpio252 -> ../../devices/6000d000.gpio/gpiochip0/gpio/gpio252
lrwxrwxrwx 1 root gpio    0 Feb 26 13:45 gpiochip0 -> ../../devices/6000d000.gpio/gpio/gpiochip0
lrwxrwxrwx 1 root gpio    0 Feb 26 13:45 gpiochip504 -> ../../devices/7000d000.i2c/i2c-4/4-003c/max77620-gpio/gpio/gpiochip504
--w--w---- 1 root gpio 4096 Feb 26 13:58 unexport

如果我用 python 做同样的事情,我会得到正确的用户和错误的组。

>>> import pathlib
>>> src = "/sys/class/gpio/gpio252"
>>> 
>>> pth = pathlib.Path(src)
>>> 
>>> pth.group()
'root'
>>> pth.owner()
'root'
>>> 

我在 Python 版本 3.6.9 和 3.9.2 上尝试过,无论有没有虚拟环境。

我有一个 UDEV 规则,可以使用 chown 命令纠正一组新创建的 gpio。但是需要一些时间,所以我想在我的代码中等待它,但我不想使用固定计时器。

谢谢您的建议

解决方法

/sys/class/gpio/gpio252 是指向不同文件的符号链接,python 返回文件中的信息,而不是符号链接。

检查组:

ls -ld /sys/devices/6000d000.gpio/gpiochip0/gpio/gpio252

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...