Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

python - WindowsError: [Error 2] The system cannot find the file specified

I am having a problem with this code. 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? I just promised someone I could do this in 5 minutes because I had a code! Shame it doesnt work!!

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))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 'dirsub+1', 'dirsub_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 SettingsDuffABureaubladJohan10G304655_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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...