if len(str(i))==1:
filename = "0000"+str(i)
if len(str(i))==2:
filename = "000"+str(i)
if len(str(i))==3:
filename = "00"+str(i)
if len(str(i))==4:
filename = "0"+str(i)
if len(str(i))==5:
filename=str(i)
Our OCR bot uses ocr.space -- one of the best API's in the world -- however it is still not reliable enough to automatically put out transcriptions that haven't been vetted and formatted by a human. Some people use its attempts as a basis, others prefer to do it manually. With code, it especially struggles -- part of it's due to the indentation not being preserved, but mostly I think it's just trained on non-monospace fonts, so it recognises them easier than monospace ones.
Fun fact, this isn't actually an issue in windows anymore. I tried to make a file naming system that exploited that feature, but it turned out they fixed it so I wound up having to pad right instead.
I'm relatively new to programming. I don't quite understand what this code is doing, but it seems to be renaming files based on the number of characters in their name?
why?
What's the correct way to do this, if this isn't the right way?
You can also utilize string formatting to make your code as spooky as possible:
filename = f"{i:0>5}"
You can also change > to ^ (center), or < (left aligned, right padded). It looks ugly but once you get the hang of it it gets really handy for quick text magic.
Eh, it depends. For python dev I think it's fine because a lot of that language is syntactic sugar and it's built with that feature in mind for a reason. Once you see it once or twice it doesn't screw you up too much.
Now for-else statements, that's a construct that messes me up every time.
In python the for statement allows you to have an else clause which is triggered if the for loop isn't broken. So instead of having a wasBroken variable you set right before a break and check after the loop you have some neat, concise syntax which replaces all that.
31
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Nov 01 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
32
u/[deleted] Nov 01 '21
I did this a lot (for formatting time) before knowing about padding functions. And it worked so I don’t care
8
u/erinaceus_ Nov 01 '21
Maybe written right at the time of the leftpad debacle?
6
u/the_surfing_llama Nov 01 '21
yeah like wtf they dont have a condition for len(str(I))==0
4
u/GaturCat Nov 02 '21
whats the purpose of this code btw
(I am a noob)
3
u/Grubzer Nov 02 '21
//TODO: Make work for all numbers
1
u/serendipitybot Nov 02 '21
This submission has been randomly featured in /r/serendipity, a bot-driven subreddit discovery engine. More here: /r/Serendipity/comments/qkzmco/did_someone_order_bad_code_xpost_from_rbadcode/
1
u/SlightlyNotMad Nov 02 '21
I'll take 00003 of bad code, thank you.
1
u/[deleted] Nov 30 '21
I'm relatively new to programming. I don't quite understand what this code is doing, but it seems to be renaming files based on the number of characters in their name?
1
u/RedditAcc-92975 Nov 01 '21
ITT people who don't know python and don't visit SO.
https://stackoverflow.com/a/339013
0
u/CopperyMarrow15 Nov 01 '21
any simpler ways I don't know about?
0
u/[deleted] Nov 01 '21
[deleted] — view deleted comment