How to change files names randomly? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to change files names randomly?

I have folder contain images , I want to put them in new folder then loop through them to: -generate a random name from the English letters -rename the images inside the 'new folder' directory to the generated one

19th Apr 2022, 1:15 PM
kareem nada
kareem nada - avatar
6 Answers
+ 3
You can do it all with the os module learn it https://docs.python.org/3/library/os.html You will need os.path, os.mkdirs, os.cwd, and some checking or post you attempts someone may help you with it
19th Apr 2022, 1:29 PM
Ion Kare
Ion Kare - avatar
+ 1
# i guess this method will do: c = 4 # number of tries if file exist for i in (list of names in directory): tmp = random name.png # or whatever while c: # so if c == 0, then loop stops add file as `i[0]+tmp[1:]`.png c-=1
20th Apr 2022, 12:16 PM
Ervis Meta
Ervis Meta - avatar
0
Eh tricky ... you can use: 1- os.system() and random to send commands to shell, or; 2- Ion Kare 's answer can be used as general.
19th Apr 2022, 6:32 PM
Ervis Meta
Ervis Meta - avatar
20th Apr 2022, 11:44 AM
kareem nada
kareem nada - avatar
0
That looks alright.
20th Apr 2022, 11:56 AM
Ervis Meta
Ervis Meta - avatar
0
Ervis Meta - SoloHelper It worked with me but had a little problem : It working on some files then stop because name been exist in folder so os.rename stop I want to make it looping if created file name 'u' then found it then pass and creates name 'uv' or 'uZ' or whatever
20th Apr 2022, 12:09 PM
kareem nada
kareem nada - avatar