python os.rename(...)将无法正常工作! [英] python os.rename(...) won't work !
问题描述
我正在编写一个Python函数,将文件列表的扩展名更改为另一个扩展名,例如将txt更改为rar,这只是一个闲置的示例.但是我遇到了一个错误.代码是:
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:\Users\B\Desktop\sil\sil2')
#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:\Users\B\Desktop\dTask.py", line 19, in dTask
os.rename(file_list[entry_pos], new_file_name)
WindowsError: [Error 2] The system cannot find the file specified
如打印输出中所示,我可以成功获得可变级别的另一个扩展名的文件名,但实际上不行,因为我无法在OS级别结束此过程.错误来自os.rename(...).知道如何解决这个问题吗?
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 ?
推荐答案
-
正如其他人所述,您要么需要提供这些文件的路径,要么切换当前工作目录,以便操作系统可以找到这些文件.
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.
++entry_pos
不执行任何操作. Python中没有增量运算符.前缀+
恰好具有前缀-
对称.用两个+
前缀只是两个无操作.因此,您实际上并没有执行任何操作(将其更改为entry_pos += 1
之后,您仍会在每次迭代中将其重置为零.
++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.
此外,您的代码也非常笨拙-例如,您正在使用file_list
的单独索引,即使仅使用该变量,也无法使其与迭代变量file_name
保持同步!为了展示如何更好地做到这一点.
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', '...')
这篇关于python os.rename(...)将无法正常工作!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!