I am downloading files from a internet source, that periodically change one specific string. Ex: from "L8" to "L6" In the file:
VT_OTT-V4-HGTHI-L6T09_T4_2682.nc
VT_OTT-V4-HGTHI-L6T09_T4_2683.nc
VT_OTT-V4-HGTHI-L6T09_T4_2684.nc
VT_OTT-V4-HGTHI-L4T12_T4_2685.nc
VT_OTT-V4-HGTHI-L9T07_T4_2686.nc
VT_OTT-V4-HGTHI-L4T12_T4_2687.nc
and so on...
I order to download files properly, I write the following line:
Level = int((path[path.find("L4T" or "L6T" or "L9T")+3:path.find("_T4")]))
(Level is changing, too, but I assign as a two-digit integer)
However, I get the error:
ValueError: invalid literal for int() with base 10: '\\\\folder\\\\folder\\\\VT_OTT-V4-HGTHI-L4T09'
Traceback (most recent call last):
File "C:\folder\folder\script.py", line xx, in <module>
Level = int((path[path.find("L4T" or "L6T" or "L9T")+3:path.find("_T4")]))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: '\\\\folder\\\\folder\\\\VT_OTT-V4-HGTHI-L4T09'
How would be the right syntax?
Here's the root of your problem:
path.find("L4T" or "L6T" or "L9T")
This part is equivalent to path.find("L4T")
since "L4T"
is a non-empty string and therefore "truthy." (Try print("L4T" or "L6T" or "L9T")
to prove it to yourself.) The reason? or
does not work the way you think it works, and function calls definitely do not work the way you think they work. (The argument to find
is fully evaluated before find
sees it, there's no way for find
to see the or
s in it.)
str.find
returns -1
if it can't find the substring. Since you are actually only looking for "L4T"
, when the string doesn't contain that, you are throwing -1
into the calculation, which means (after adding 3) your slice starts at index 2 of the path rather than the position of LxT
. So you're actually trying to pass most of the path to int()
and as you've seen, that doesn't work because that's not a number.
One way to solve this issue is with a regular expression:
re.search("L4T|L6T|L9T", path).start()
Or the equivalent:
re.search("L[469]T", path).start()
If you prefer not to use a regular expression, you could use max()
to find the result that isn't -1
:
max(path.find("L4T"), path.find("L6T"), path.find("L9T"))
Or, here is an excessively clever bit that relies on two of the find
s returning -1
. Don't actually do this, it's just for fun:
path.find("L4T") * path.find("L6T") * path.find("L9T")