Python Forum
Rename folders as files inside
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rename folders as files inside
#1
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!
Reply
#2
what have you tried?
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using zipfile module - finding folders not files darter1010 2 258 Apr-06-2024, 07:22 AM
Last Post: Pedroski55
  Rename files in a folder named using windows explorer hitoxman 3 737 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,484 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  Create new folders and copy files cocobolli 3 1,445 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 1,002 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  Rename part of filename in multiple files atomxkai 7 7,329 Feb-18-2022, 10:03 PM
Last Post: atomxkai
  Rename Files based on XML file klturi421 3 2,182 Oct-22-2021, 07:37 PM
Last Post: klturi421
  Moving files to Folders giddyhead 13 9,134 Mar-07-2021, 02:50 AM
Last Post: giddyhead
  Rename Multiple files in directory to remove special characters nyawadasi 9 6,367 Feb-16-2021, 09:49 PM
Last Post: BashBedlam
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 1,857 Dec-06-2020, 04:14 PM
Last Post: Divya577

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020