有时我们下载一个很大的资源,里面文件很多,还有一层层的子文件夹,好不容易下载到99%不动了,怎么办?
其实,这时大部分文件都已经下载成功了,如果不在意少数没下载完的文件, 可以把下载完的文件的后缀 bt.xltd去掉,但是如何用代码来批量操作?
我写了一段代码,完美运行,如下:
import os
root_dir = r'D:\temp'
def list_files(root_dir):
if os.path.isfile(root_dir): #file
# 修改文件名
newFilename = root_dir.strip('.bt.xltd')
os.rename(root_dir, newFilename)
print(root_dir)
else: #dir
for file in os.listdir(root_dir):
file_path = os.path.join(root_dir, file)
if os.path.isfile(file_path): #file
#修改文件名
newFilename = file_path.strip('.bt.xltd')
os.rename(file_path, newFilename)
print(file_path)
else: #dir
list_files(file_path)
if __name__ == '__main__':
list_files(root_dir)