简体   繁体   中英

Get a complete name of a directory in Python

My SO is Raspbian in a Raspberry Pi 2. Im working with nano 2.2.6 Im a newbie in Python. I want to program a temperature sensor D18B20. I got a fantastic manual: Raspberry Pi temperature sensor . When I list the devices with:

  ls -l /sys/bus/w1/devices/

I would like to get the name of the directory asociated to the thermometer as 28*. I know the name but i need to make a program to get the name. I was working with this code. But i can't get the full name of the directory.

  import os
  name = os.path.basename ("sys/bus/w1/devices/28*")
  print (name)

Thank you very much for your time and patience Best regards

Edited Version 0.1b

import glob, os.path
import time

paths = glob.glob("/sys/bus/w1/devices/28-*")
path_names= [os.path.basename(path) for path in paths]
l = list (path_names)
name = l[0] 
path_names = "".join(name)
print name

while 1:
    tempfile = open ("/sys/bus/w1/devices/name/w1_slave")
    #thetext = tempfile.read()
    #tempfile.close()
    #tempdata = thetext.split("\n"[1].split(" ")[9]
    #temperature = float(tempdata[2:])
    #temperature = temperature / 1000
    #print temperature
    #time.sleep (1)

Based on a google search of your link, it seems like your temperature sensor follows a naming pattern of 28-*. You could use this:

import glob, os.path
paths = glob.glob("/sys/bus/w1/devices/28-*")
path_names = [os.path.basename(path) for path in paths]

if len(path_names) > 0:
   first_sensor = os.path.normpath(os.path.join(path_names[0], "w1_slave"))
   while True:
       # read from sensor file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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