13

I am trying to rename all of the filenames within a folder so that they no longer have +'s in them! This has worked many times before but suddenly I get the error:

WindowsError: [Error 2] The system cannot find the file specified at line 26

Line 26 is the last line in the code.

Does anyone know why this is happening?

import os, glob, sys
folder = "C:\\Documents and Settings\\DuffA\\Bureaublad\\Johan\\10G304655_1"

for root, dirs, filenames in os.walk(folder):
    for filename in filenames:
        filename = os.path.join(root, filename)
old = "+"
new = "_"
for root, dirs, filenames in os.walk(folder):
    for filename in filenames:
        if old in filename:
            print (filename)
            os.rename(filename, filename.replace(old,new))
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Alice Duff
  • 667
  • 6
  • 11
  • 18
  • 5
    Just a minor comment. When updating the question to add new information, it helps other readers if you leave the original question there, and just add the new bits, so we can see the history. – Simon Callan Mar 16 '11 at 13:20
  • Just to note, the first loop doesn't seem to have any affect. All the variables it creates are overwritten in the next loop. – wjandrea Jul 15 '23 at 17:23

2 Answers2

12

I suspect that you may be having issues with subdirectories.

If you have a directory with files "a", "b" and subdirectory "dir" with files "sub+1" and "sub+2", the call to os.walk() will yield the following values:

(('.',), ('dir',), ('a', 'b'))
(('dir',), (,), ('sub+1', 'sub+2'))

When you process the second tuple, you will call rename() with 'sub+1', 'sub_1' as the arguments, when what you want is 'dir\\sub+1', 'dir\\sub_1'.

To fix this, change the loop in your code to:

for root, dirs, filenames in os.walk(folder):
    for filename in filenames:           
        filename = os.path.join(root, filename)
        ... process file here

which will concatenate the directory with the filename before you do anything with it.

Edit:

I think the above is the right answer, but not quite the right reason.

Assuming you have a file "File+1" in the directory, os.walk() will return

("C:/Documents and Settings/DuffA/Bureaublad/Johan/10G304655_1/", (,), ("File+1",))

Unless you are in the "10G304655_1" directory, when you call rename(), the file "File+1" will not be found in the current directory, as that is not the same as the directory os.walk() is looking in. By doing the call to os.path.join() yuo are telling rename to look in the right directory.

Edit 2

A example of the code required might be:

import os

# Use a raw string, to reduce errors with \ characters.
folder = r"C:\Documents and Settings\DuffA\Bureaublad\Johan\10G304655_1"

old = '+'
new = '_'

for root, dirs, filenames in os.walk(folder):
 for filename in filenames:
    if old in filename: # If a '+' in the filename
      filename = os.path.join(root, filename) # Get the absolute path to the file.
      print (filename)
      os.rename(filename, filename.replace(old,new)) # Rename the file
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Simon Callan
  • 3,020
  • 1
  • 23
  • 34
  • do you mean like this?import os, glob, sys folder = "C:\\Documents and Settings\\DuffA\\Bureaublad\\Johan\\10G304655_1" for root, dirs, filenames in os.walk(folder): for filename in filenames: filename = os.path.join(root, filename) old = "+" new = "_" for root, dirs, filenames in os.walk(folder): for filename in filenames: if old in filename: print (filename) os.rename(filename, filename.replace(old,new)) – Alice Duff Mar 16 '11 at 13:04
  • i have added the code into my original question! Im sorry if these are really basic mistakes i am making! I am having to learn this myself with no resources!! – Alice Duff Mar 16 '11 at 13:06
4

You are using splitext to determine the source filename to rename:

filename_split = os.path.splitext(filename) # filename and extensionname (extension in [1])
filename_zero = filename_split[0]#
...
os.rename(filename_zero, filename_zero.replace('+','_'))

If you encounter a file with an extension, obviously, trying to rename the filename without the extension will lead to a "file not found" error.

Jim Brissom
  • 31,821
  • 4
  • 39
  • 33
  • I tried running it again, and not splitting the file but i get the same error as before - i edited my question to include the other script i used! – Alice Duff Mar 16 '11 at 11:05