I’m relatively new to Python’s string handling and have troubles figuring out how to solve this: I have absolute path along the lines of /dir/MAC.timestamp.bin that i’m looping through with something like:
for fh in glob.glob(DATA_FOLDER+"*.bin"):
retval = database.postdata(fh)
And what I need now, is to extract the MAC (WHICH COMES IN 6 characters).
I was thinking of doing something along the lines of
for fh in glob.glob("bin/*.bin"):
list=fh.split("/")
lstlen=len(list)
mac=list[lstlen-1][:6]
retval = database.postdata(mac,fh)
I’m however not 100% sure if that will be air tight at all times and if there’s a better way to handle this?
Any hints are appreciated!
Thank you!
That should work.
Python lists can be index with negative numbers. with
-1being the last element in the list.Again, as your question states my solution, and yours, will not be air tight if the incoming data does not conform to your specifications. You will need to add in a check such as the following:
This check for it would also benefit from checking that it does not contain characters that produce a invalid
macin your case.