Get full directory name in Python

My SO is Raspbian in Raspberry Pi 2. Im working with nano 2.2.6 I am new to Python. I want to program the temperature sensor D18B20. I got a fantastic guide: Raspberry Pi Temperature Sensor . When I list devices using:

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

I would like to get the name of the directory associated with the thermometer as 28 *. I know the name, but I need to make a program to get the name. I worked with this code. But I can not get the full name of the directory.

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

Thanks so 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)
+4
source share
1 answer

Based on a search for your link on Google, it appears that your temperature sensor follows the 28- * naming pattern. You can 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
0
source

All Articles