Python Forum
how to increment all file names in a folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to increment all file names in a folder
#1
how do i create a short script to change all file names in a folder with an increment?

is there a guide or a simple rule?

want to use it as a practice for coding with python i am a beginner
Reply
#2
You want the os module. It has a rename function for renaming files, and a listdir function for finding the files in a folder (or a walk function for doing it recursively). The re module could be helpful for finding the index in the file name, you could do it with the isdigit method of strings. Or if it's consistently the same number of digits at the end of a file, you could just use slicing.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Can pick up some tips in this recent post.
You can also look into pathlib,it's a new way to handle filesystem paths ect...
Reply
#4
are the file names just numbers like "12345678" or do they partly have numbers like "data2018"?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
(Sep-09-2018, 06:43 AM)Skaperen Wrote: are the file names just numbers like "12345678" or do they partly have numbers like "data2018"?
All the files in the folder got the exact same name which is a pain to work with, there are thousands of them so I probably need to use a loop right?

I need it to automatically go from file to file and add a number to each of them like so:
file.

file1.

file2.

and so on...
Reply
#6
so you need to make a mass rename. yeah that obviously needs a loop. you could write a script to do it. you could do it in Python, but in a shell script might be easier if you know shell scripting. you could do it in Python as a learning exercise. me, personally, i'd do it in a command (in bash) in that directory like:

(ls -1|while read f;do mv $f ${f}1;done)

if you want the "1" to be before the "." it will be a slightly longer command. but maybe it is better for you to do this as a Python script. write something that outputs the commands to do it and when it makes all the right commands then pipe them into a shell.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 523 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Reading a file name fron a folder on my desktop Fiona 4 890 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  help to increment a third list hermine 7 1,316 Nov-29-2022, 04:19 PM
Last Post: perfringo
  rename same file names in different directories elnk 0 706 Nov-04-2022, 05:23 PM
Last Post: elnk
  mysql id auto increment not working tantony 10 2,393 Oct-18-2022, 11:43 PM
Last Post: Pedroski55
  Function not executing each file in folder mathew_31 9 2,227 Aug-22-2022, 08:40 PM
Last Post: deanhystad
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 6,001 Jan-07-2022, 04:55 PM
Last Post: bowlofred
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,926 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,465 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  How to import file and function in another folder SriRajesh 1 3,140 Dec-18-2021, 08:35 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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