0

So I got this working, but now I would like some help to make it read folders and subfolders

Also how can I make this change multiple files at one time

This is what I got going

import glob
import os

for f in glob.glob('*.txt'):
    new_filename = f.replace("(1)","(01)")
    new_filename = new_filename
    os.rename(f,new_filename)

I have over 80,000 .txt files all found in many different folders and Subs and I want to edit the file name on every txt file with (1), (2), (3), (4) and so on to (01), (02), (03), and so on

if it's possible to make it read all these files and edit the them using one script that would be great

right now I have made 9 filenamechanger.py scripts and I am placing them in each folder 1 by 1

I did some research on how to make this read subfolders, but I can't get it going

I tried several ways but can't get it going this was the last thing I tried

for f in glob.glob("./*.txt/")
for f in glob.glob("./*/ *.txt")

Any help, even if it's only Sun Folder reading that would be great

Thank you

JR Santos
  • 137
  • 1
  • 11

1 Answers1

0

You can use a double asterisk, which matches any level of directories:

for f in glob.glob("*/**/*.txt"):
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • @jodag: Yeah, that's what I meant. – mipadi Jan 28 '22 at 00:00
  • Not sure you can see this since the Question was closed - but Thank you this helped to understand my mistake I have many folders so I did this ` for f in glob.glob("*/**/***/****/*.txt"):` and this worked @jodag, @mipadi – JR Santos Jan 28 '22 at 16:34
  • Never mind this `("/**/***/****/.txt"):` does not work, this only counts certain folders – JR Santos Jan 28 '22 at 17:07
  • Man, python is great, but is terrible with the different version, well I'll just go back to batch... Thanks anyways guys – JR Santos Jan 28 '22 at 17:53