擅长:python、mysql、java
<ol>
<li><p>正如其他人已经指出的,您要么需要提供这些文件的路径,要么切换当前工作目录,以便操作系统能够找到这些文件。</p></li>
<li><p><code>++entry_pos</code>什么都不做。Python中没有increment运算符。前缀<code>+</code>与前缀<code>-</code>正对称。在某物前面加上两个<code>+</code>只是两个no ops。所以你实际上什么也没做(在你把它改成<code>entry_pos += 1</code>之后,你仍然在每次迭代中将它重置为零。</p></li>
<li><p>另外,您的代码非常不优雅-例如,您正在使用一个单独的索引来<code>file_list</code>,并且无法使其与迭代变量<code>file_name</code>保持同步,即使您可以只使用那个索引!以显示如何能做得更好。</p></li>
</ol>
<p>-</p>
<pre><code>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', '...')
</code></pre>