3

Could you please help me to modify below script to change the name of files also in subdirectories.

def change():
    path = e.get()
    for filename in os.walk(path):
        for ele in filename:
            if type(ele) == type([]) and len(ele)!=0:
                for every_file in ele:

                    if every_file[0:6].isdigit():
                        number = every_file[0:6]
                        name = every_file[6:]
                        x = int(number)+y
                        newname = (str(x) + name)
                        os.rename(os.path.join(path, every_file), os.path.join(path, newname))
tobias_k
  • 81,265
  • 12
  • 120
  • 179
MaciejPL
  • 1,017
  • 2
  • 9
  • 16

1 Answers1

14

I don't know what constraints you have on file names, therefore I wrote a general script just to show you how change their names in a given folder and all subfolders.

The test folder has the following tree structure:

~/test$ tree
.
├── bye.txt
├── hello.txt
├── subtest
│   ├── hey.txt
│   ├── lol.txt
│   └── subsubtest
│       └── good.txt
└── subtest2
    └── bad.txt

3 directories, 6 files

As you can see all files have .txt extension.

The script that rename all of them is the following:

import os


def main():
    path = "/path/toyour/folder"
    count = 1

    for root, dirs, files in os.walk(path):
        for i in files:
            os.rename(os.path.join(root, i), os.path.join(root, "changed" + str(count) + ".txt"))
            count += 1


if __name__ == '__main__':
    main()

The count variable is useful only to have different names for every file; probably you can get rid of it.

After executing the script, the folder looks like this:

~/test$ tree
.
├── changed1.txt
├── changed2.txt
├── subtest
│   ├── changed4.txt
│   ├── changed5.txt
│   └── subsubtest
│       └── changed6.txt
└── subtest2
    └── changed3.txt

3 directories, 6 files

I think that the problem in your code is that you don't use the actual root of the os.walk function.

Hope this helps.

Alberto Coletta
  • 1,563
  • 2
  • 15
  • 24
  • Hi is there a chance you could modify my script to add this? After giving a path it should go trough all subfolders to change the name. – MaciejPL Jan 14 '15 at 12:14
  • 3
    No way. I show you how to do that as a general procedure, now it's your turn ;) What I suggest to you is to read carefully the python documentation about `os.walk` function, I don't think you undestand it correctly. Looking at your code, I suggest also to read some python book (e.g. [link](http://learnpythonthehardway.org)). – Alberto Coletta Jan 14 '15 at 14:34
  • @MaciejPL at least try :) – Alberto Coletta Jan 15 '15 at 10:08
  • I know what was wrong with my code. path.join was using given path from input not the one that was listed by os.walk – MaciejPL Jan 15 '15 at 12:58
  • Can someone explain me what does this part do? My cos is working without it but my freind tells be that this needs to stay. for ele in filename: if type(ele) == type([]) and len(ele)!=0: – MaciejPL Jan 15 '15 at 13:01