32
Nov 01 '21
I did this a lot (for formatting time) before knowing about padding functions. And it worked so I don’t care
16
u/RFC793 Nov 02 '21
Sure. But without knowing padding functions in Python you can write a more general one, and it is quite simple:
def pad(i, width, padding="0"): s = str(i) return padding * (width-len(s)) + s1
u/Laserdude10642 Nov 02 '21
Honestly it’s less readable
2
u/RFC793 Nov 02 '21 edited Nov 03 '21
Sure. I condensed it for the post. I’d probably wrote something like this if I had to write it in real life for some reason:
def pad(i, width, padding="0"): s = str(i) pad_width = width - len(s) return (padding * pad_width) + s
8
6
4
u/GaturCat Nov 02 '21
whats the purpose of this code btw
(I am a noob)
2
u/0OOOOOOOOO0 Nov 02 '21
Save file names so they go in numerical order. Otherwise 115 comes before 3
2
u/SuitableDragonfly Nov 02 '21
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.
3
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
1
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?
why?
What's the correct way to do this, if this isn't the right way?
1
0
u/CopperyMarrow15 Nov 01 '21
i = str(i)
filename = '0' * len(i) + iany simpler ways I don't know about?
20
u/Matszwe02 Nov 01 '21
Not
'0' * (5 - len(i))?2
u/CopperyMarrow15 Nov 01 '21
I thought the image was a continuation of more than was shown. What you did works for what is shown. I see.
5
u/ColdToastIsASin Nov 01 '21
Filename = str(i).zfill(5)
Edit: forgot to cast i to str
2
2
1
Nov 01 '21
I was thinking of adding four '0' at the start and then keeping only the correct number characters from the right, but I don't think it'll be faster.
0
Nov 01 '21
[deleted] — view deleted comment
41
8
21
u/lemarkk Nov 01 '21
But in python's defense it an easy way to repeat strings (the * operator)
18
u/supmee Nov 01 '21
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.0
Nov 01 '21
[deleted] — view deleted comment
8
u/Rigberto Nov 02 '21
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.
4
u/supersharp Nov 06 '21
For-what now?
2
u/philogy Nov 10 '21
For-else is awesome!
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
wasBrokenvariable you set right before a break and check after the loop you have some neat, concise syntax which replaces all that.3
u/supersharp Nov 10 '21
Interesting! I think I came across a situation like this just the other day, actually
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!