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 57 times
1
-
1Possible duplicate of [Rename files sequentially in python](https://stackoverflow.com/questions/45286364/rename-files-sequentially-in-python) – black_fm Jul 31 '18 at 06:52
-
You can do it in bash: `paste – 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,048
- 3
- 24
- 42
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
- 3,747
- 2
- 12
- 22
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
- 476
- 7
- 20