I changed your code for the detection of a url because it was rasing a ValueError saying this isn't a url if the file was not found. I kept the same detection method as you used though.