Python Forum
Rename folders as files inside - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Rename folders as files inside (/thread-14576.html)



Rename folders as files inside - Ral1s - Dec-07-2018

Hi,
i have a problem with rename a lot of folders.

There is a structure of my problem and what i need..

I have many folders..
for example:

folder /0512-2018 contains files 78563412.txt and 78563412.rar

--- 0512-2018 ---
-- 78563412.txt
-- 78563412.rar
---------------------
and i need rename this folder as files inside this folder:

--- 78563412 ---
-- 78563412.txt
-- 78563412.rar
---------------------

Thanks so much!


RE: Rename folders as files inside - Larz60+ - Dec-07-2018

what have you tried?


RE: Rename folders as files inside - jeanMichelBain - Dec-07-2018

Maybe there is somewhere a magic module doing the job...
Anyway, you can do it with the os module:

import os,os.path
for dirname in os.listdir('.'):
	if os.path.isdir(dirname):
		for filename in os.listdir(dirname):
			newname = filename.split('.')[0]
			os.rename(dirname,newname)
			break