I have a folder of images, they have random names. What i want to do is change the images names to numbers for example 1.jpg 2.jpg 3.jpg and so on till the images are done.
Asked
Active
Viewed 65 times
1
-
1Possible duplicate of [Rename files sequentially in python](https://stackoverflow.com/questions/45286364/rename-files-sequentially-in-python) – kvoki Jul 31 '18 at 06:52
-
You can do it in bash: `paste <(ls) <(for i in $(seq $(ls -1 | wc -l)); do echo $i.jpg; done) | xargs -l mv` – Reut Sharabani Jul 31 '18 at 07:03
3 Answers
1
what you need is os.listdir()
to list all items in a folder and os.rename
to rename those items.
import os
contents = os.listdir()
for i, filename in enumerate(contents):
os.rename(filename, i) # or i.jpg or whatever which is beyond that scope

marmeladze
- 6,468
- 3
- 24
- 45
0
import os
path = '/Path/To/Directory'
files = os.listdir(path)
i = 1
for file in files:
os.rename(os.path.join(path, file), os.path.join(path, str(i)+'.jpg'))
i = i+1

Ninad Gaikwad
- 4,272
- 2
- 13
- 23
0
This can be done using os
library:
If the folder has images only, no other files, you can run in the correct folder:
import os
count = 1
for picture in os.listdir():
os.rename(picture, f'{count}.jpg')
count += 1
You can read more about os
here: https://docs.python.org/3/library/os.html

johnnyheineken
- 543
- 7
- 20