Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python os.rename(...) won't work !

I am writing a Python function to change the extension of a list of files into another extension, like txt into rar, that's just an idle example. But I'm getting an error. The code is:

import os
def dTask():
    #Get a file name list
    file_list = os.listdir('C:UsersBDesktopsilsil2')
    #Change the extensions
    for file_name in file_list:
        entry_pos = 0;
        #Filter the file name first for '.'
        for position in range(0, len(file_name)):
            if file_name[position] == '.':
                break
        new_file_name = file_name[0:position]
        #Filtering done !
        #Using the name filtered, add extension to that name
        new_file_name = new_file_name + '.rar'
        #rename the entry in the file list, using new file name
        print 'Expected change from: ', file_list[entry_pos]
        print 'into File name: ', new_file_name
        os.rename(file_list[entry_pos], new_file_name)
        ++entry_pos
Error:
>>> dTask()
Expected change from:  New Text Document (2).txt
into File name:  New Text Document (2).rar
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    dTask()
  File "C:UsersBDesktopdTask.py", line 19, in dTask
    os.rename(file_list[entry_pos], new_file_name)
WindowsError: [Error 2] The system cannot find the file specified

I can succeed in getting the file name with another extension in variable level as you can see in the print-out, but not in reality because I can not end this process in OS level. The error is coming from os.rename(...). Any idea how to fix this ?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)
  1. As the others have already stated, you either need to provide the path to those files or switch the current working directory so the os can find the files.

  2. ++entry_pos doesn't do anything. There is no increment operator in Python. Prefix + is just there fore symmetry with prefix -. Prefixing something with two + is just two no-ops. So you're not actually doing anything (and after you change it to entry_pos += 1, you're still resetting it to zero in each iteration.

  3. Also, your code is very inelegant - for example, you are using a separate index to file_list and fail to keep that in synch with the iteration variable file_name, even though you could just use that one! To show how this can be done better.

-

def rename_by_ext(to_ext, path):
    if to_ext[0] != '.':
        to_ext = '.'+to_ext
    print "Renaming files in", path
    for file_name in os.listdir(path):
        root, ext = os.path.splitext(file_name)
        print "Renaming", file_name, "to", root+ext
        os.rename(os.path.join(path, file_name), os.path.join(path, root+to_ext))
rename_by_ext('.rar', '...')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...