Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check presence of a file
#2
There are 2 ways you can do this:
1) Check if the file exist and then open it (typical from other languages like C)
2) try to open it and capture the the error ("ask forgiveness not permission", normal in languages with exceptions like python)
The 2nd one has also big advantages when you add security concerns...
In this case your code can be rewritten as:
def read_ds18b20(sensorid):
    """
    Functon reads DS15B20 sensor
    """
    device = f"/sys/bus/w1/devices/{sensorid}/w1_slave"
    try:
        # No need to close, the with block does it for us...
        with open(device, 'rt') as tfile:
            text = tfile.read()
    except FileNotFoundError:
        # Things to do when the sensor id is invalid...
        # I just going to return None.
        return None
    except PermissionError:
        # The file exists, but the user is not in the right group
        print(f"You must be in the right group to access:\n{device}")
        # Just continue with the error...
        raise
    # Ok, everything was correct, let's continue
    # Select 21st word starting from 0.
    sensor_data =text.split(" ")[20] 
    # Remove 't=' and make floating.
    temp = float(sensor_data[2:]) / 1000
    #return rounded to 1 decimal
    return round(temp, 1)
I am using 2 features that are from recent versions of python, the string interpolation f"bla bla {variable} bla bla" that you can change it to "bla bla {} bla bla".format(variable) and the new exceptions FileNotFoundError and PermissionError... if your python is too old and returns other exception, just experiment from the terminal and use the proper ones...
Reply


Messages In This Thread
Check presence of a file - by Steffenwolt - May-20-2018, 04:28 PM
RE: Check presence of a file - by killerrex - May-20-2018, 05:58 PM
RE: Check presence of a file - by Steffenwolt - May-21-2018, 09:37 AM
RE: Check presence of a file - by buran - May-21-2018, 09:38 AM
RE: Check presence of a file - by Steffenwolt - May-21-2018, 03:58 PM
RE: Check presence of a file - by killerrex - May-21-2018, 05:43 PM
RE: Check presence of a file - by Steffenwolt - May-23-2018, 08:31 PM
RE: Check presence of a file - by killerrex - May-24-2018, 07:50 AM
RE: Check presence of a file - by Steffenwolt - May-25-2018, 07:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  please check this i wanna use a csv file as a graph xCj11 5 1,509 Aug-25-2022, 08:19 PM
Last Post: deanhystad
  check if a file exist on the internet and get the size kucingkembar 6 1,806 Apr-16-2022, 05:09 PM
Last Post: kucingkembar
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,955 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  Check last time file was accessed Pavel_47 4 2,846 Jun-01-2021, 05:47 PM
Last Post: Yoriz
  How to check if a file has finished being written leocsmith 2 7,875 Apr-14-2021, 04:21 PM
Last Post: perfringo
  Check if a file exists. Pedroski55 5 3,329 Sep-08-2020, 10:01 AM
Last Post: Pedroski55
  How to check to see a dbf file is EOF ? DarkCoder2020 0 1,736 Jun-16-2020, 05:03 PM
Last Post: DarkCoder2020
  Building a script to check size of file upon creation mightyn00b 2 2,398 Apr-04-2020, 04:39 AM
Last Post: Larz60+
  MySql - Loop - CHeck File gcclinux 1 2,100 Nov-30-2019, 11:51 AM
Last Post: ThomasL
  Checking the presence of label using pywinauto module Malt 0 1,941 Jul-26-2019, 09:06 AM
Last Post: Malt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020