1

I need to simply add the word "_Manual" onto the end of all the files i have in a specific directory Here is the script i am using at the moment - i have no experience with python so this script is a frankenstine of other scripts i had lying around!

It doesn't give any error messages but it also doesnt work..

folder = "C:\Documents and Settings\DuffA\Bureaublad\test"

import os, glob

for root, dirs, filenames in os.walk(folder):
    for filename in filenames:
        filename_split = os.path.splitext(filename) # filename and extensionname (extension in [1])
        filename_zero = filename_split[0]
        os.rename(filename_zero, filename_zero + "_manual")

I am now using

folder = "C:\Documents and Settings\DuffA\Bureaublad\test"
import os # glob is unnecessary
for root, dirs, filenames in os.walk(folder):
    for filename in filenames:
        fullpath = os.path.join(root, filename)
        filename_split = os.path.splitext(fullpath) # filename and extensionname (extension in [1])
        filename_zero, fileext = filename_split
        print fullpath, filename_zero + "_manual" + fileext
        os.rename(fullpath, filename_zero + "_manual" + fileext)

but it still doesnt work.. it doesnt print anything and nothing gets changed in the folder!

Alice Duff
  • 667
  • 6
  • 11
  • 18

4 Answers4

5

os.rename requires a source and destination filename. The variable filename contains your current filename (e.g., "something.txt"), whereas your split separates that into something and txt. As the source file to rename, you then only specify something, which fails silently.

Instead, you want to rename the file given in filename, but as you walk into subfolders as well, you need to make sure to use the absolute path. For this you can use os.path.join(root, filename).

So in the end you get something like this:

os.rename(os.path.join(root, filename), 
  os.path.join(root, filename_zero + "_manual" + filename_split[1]))

This would rename dir1/something.txt into dir1/something_manual.txt.

Frank
  • 10,461
  • 2
  • 31
  • 46
4
folder = r"C:\Documents and Settings\DuffA\Bureaublad\test"

import os, glob

for root, dirs, filenames in os.walk(folder):
    for filename in filenames:
        filename_split = os.path.splitext(filename) # filename and extensionname (extension in [1])
        filename_zero = filename_split[0]
        os.rename(os.path.join(root, filename), os.path.join(root, filename_zero + "_manual" + filename_split[1]))

In your code, you are trying to rename filename_zero, which is the filename without extension and therefore does not exist as a real path. You have to specify the full path to os.rename like above.

Jacob
  • 41,721
  • 6
  • 79
  • 81
  • Yep, there was a closing parens missing. Corrected it. – Jacob Aug 23 '11 at 07:15
  • oop - fixed that, there was a missing ")" .. but it still just silently fails? – Alice Duff Aug 23 '11 at 07:16
  • Works for me. It shouldn't fail silently, it should throw an OSError exception. Are the files currently in use by another application, do you have sufficient rights? – Jacob Aug 23 '11 at 07:18
  • It just says that is has been terminated with exit code 00000000 and nothing in the directory has been changed! – Alice Duff Aug 23 '11 at 07:21
3

I. e. it does nothing? Let's see:

folder = "C:\Documents and Settings\DuffA\Bureaublad\test"

import os # glob is unnecessary

for root, dirs, filenames in os.walk(folder):
    for filename in filenames:
        fullpath = os.path.join(root, filename)
        filename_split = os.path.splitext(fullpath) # filename and extensionname (extension in [1])
        filename_zero, fileext = filename_split
        os.rename(fullpath, filename_zero + "_manual" + fileext)

might do the trick, as you have to work with the full path. but I don't understand why there was no exception when the files could not be found...


EDIT to put the change to a more prominent place:

You as well seem to have your path wrong.

Use

folder = r"C:\Documents and Settings\DuffA\Bureaublad\test"

to prevent that the \t is turned into a tab character.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • 1
    Again, with this one i don't get an error but nothing has changed in the directory! – Alice Duff Aug 23 '11 at 07:21
  • Strange... You could put a `print fullpath, filename_zero + "_manual" + fileext´ before the `os.rename()` in order to see what happens... – glglgl Aug 23 '11 at 07:30
  • Ouch, I think I got it now: there is something wrong with your folder. Try `folder = r"C:\Documents and Settings\DuffA\Bureaublad\test"` - the `\t` inside the string is made into a tab, which you prevent with the `r` prefix. – glglgl Aug 23 '11 at 07:48
  • @glglgl Why don't you write directly: ``filename_zero, fileext = os.path.splitext(os.path.join(root, filename))`` ? – eyquem Aug 23 '11 at 07:55
0
for root, dirs, filenames in os.walk(folder):
    for filename in filenames:
        os.rename(os.path.join(root,filename),
                  os.path.join(root,'%s_manual%s' % os.path.splitext(filename)))

you should add a control in your code, to verify that the filename to rename hasn't already '_manual' in its string name

eyquem
  • 26,771
  • 7
  • 38
  • 46
  • it definately doesnt - i have made some test files with random names! – Alice Duff Aug 23 '11 at 07:44
  • @Alice Duff I was on the point to write there is a twist in your code... And now I write there WAS a twist in your code, because I've just seen that _glglgl_ has found the twist: you must escape the name of your folder. You're certainly on Windows. So my code definitely works. – eyquem Aug 23 '11 at 08:03