I need to iterate a range between 0 to 18, but in case the number is smaller than 10 then it should contain 0 (e.g. 03) in the first digit, in order to fit the file name.
files = ["part_000" i ".parquet" for i in range (0, 19)]
How do I add a condition inside the comprehensions so it would be part-00004 rather than part-0004 (04 instead of 4) etc.?
CodePudding user response:
You can modify your code as follows:
files = [f"part_{i:05d}.parquet" for i in range (19)]
print(files)
CodePudding user response:
files = ["part_000" str(i).zfill(2) ".parquet" for i in range (0, 19)]
you are looking for zfill().
EDIT: You can also just do
"part_" str(i).zfill(5) ".parquet"
