Sep-21-2020, 03:51 AM
Most functions just assume that particular type will be sent in (it's always a list-like object or it's always a string).
You can ask the object if it's an instance of a string and otherwise assume you could iterate through it.
You can ask the object if it's an instance of a string and otherwise assume you could iterate through it.
def show_strings(obj): if isinstance(obj, str): print(f"got the single string {obj}") else: for s in obj: print(f"One string in the collection is {s}") show_strings("onestring") print() show_strings(("string1", "string2", "string3"))
Output:got the single string onestring
One string in the collection is string1
One string in the collection is string2
One string in the collection is string3
I don't see any reason that it should work on local files and fail on files in /sys. Does it work for something like "/etc/passwd"? I don't have the file that you're concerned about on my system, so I don't know if it's special in some way.