16
Serious question, why does my first Python script keep crashing on one line?
I wrote a simple script to rename a folder of 50 photos, and it keeps dying at line 12 with a weird file path error. Turned out I was mixing forward slashes and backslashes from copying a Windows path straight into the code. Has anyone else tripped up on path formatting when starting out?
3 comments
Log in to join the discussion
Log In3 Comments
joel6033d ago
Yeah paths are always a pain especially on Windows.
6
holly_sanchez753d ago
Honestly, that exact Windows path problem got me so bad my first week coding. Did you actually notice the slash mix-up right away, or did it take you a while to figure out what was going on? I had to stare at my code for like 20 minutes before I saw the backslash was wrong, felt so stupid after. It's crazy how one little character can crash your whole script like that. Tbh, now I always use raw strings with an 'r' in front just to be safe on Windows.
0
lee721d ago
Wait, are you saying raw strings are actually the best fix for this? I've been coding for years and honestly I think relying on raw strings is a crutch that just masks the real issue. Python handles forward slashes perfectly fine even on Windows, so why not just train yourself to always use those instead? I mean, I see people mess up paths all the time because they lean on raw strings and then forget they have to use them, but forward slashes work everywhere without any special syntax. Doesn't that just lead to cleaner, more portable code in the long run, especially if you ever move stuff to Linux?
3