问候所以我有一个适用于根文件夹的代码。
import os, sys
path = 'root folder'
for filename in os.lestdir(os.path.dirname(path)):
base_file, ext = os.path.splitext(filename)
if ext == ".prn":
os.rename(filename,base_file + "htm")
然后我尝试使用os.walk通过子文件夹迭代它然后它在根文件夹或子文件夹中停止工作这里是代码:
import os, sys
path = 'root folder'
for roots, dirs, files in os.walk(path):
for filename in os.lestdir(os.path.dirname(path)):
base_file, ext = os.path.splitext(filename)
if ext == ".prn":
os.rename(filename,base_file + "htm")
答案 0 :(得分:1)
您已经拥有一个方便的文件名列表,因此无需再次创建它。以下是我的表现:
import os
path = 'root folder'
for subdir, dirs, files in os.walk(path):
for filename in files:
base_file, ext = os.path.splitext(filename)
if ext == ".prn":
new_name = base_file + '.htm'
os.rename(os.path.join(subdir, filename),
os.path.join(subdir, new_name))