You know how sometimes, you find a very bad code, but it manages to teach you something? I just had this funny experience in a production code in python.
It happens that you have a full path to a file and you just need the filename. You do not need to be cross platform, or care for corner cases, so you just use the split method:
You split around the slash characters, and you keep just the last part. Now I found this approach in production code:
It uses an ugly named and useless boolean value for the loop, but it also uses split with a second parameter I did not know about. So thanks to this code, I now know that you can specify the max number of splits. Here, it splits only around the first slash, keep the last part, and repeats until all slash characters are out.
Thanks for the lesson...
No comments:
Post a Comment