有時我們下載一個很大的資源,里面文件很多,還有一層層的子文件夾,好不容易下載到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)
