我写了一个Python脚本来重命名文件夹中的所有文件。代码是:
import os
import sys
import platform
walk_dir = dir_path = os.path.dirname(os.path.realpath(__file__))
print('walk_dir = ' + walk_dir)
print('walk_dir (absolute) = ' + os.path.abspath(walk_dir))
filePathList =[]
for root, subdirs, files in os.walk(walk_dir):
for filename in files:
file_path = os.path.join(root, filename)
filePathList.append(file_path)
total = len(filePathList)
print str(total) + " files found.\n\n\n"
count = 0
for file in filePathList:
count = count + 1
if file == os.path.realpath(__file__):
continue
old_file_name = file
parentDirectories = str(file).split('/')
dir_lenght = len(parentDirectories)
new_file_name = ''
index = 0
while index < dir_lenght - 1:
new_file_name = new_file_name + parentDirectories[index] + '/'
index = index + 1
new_file_name = new_file_name + parentDirectories[dir_lenght -2] + '_' + parentDirectories[dir_lenght - 1]
os.rename(old_file_name, new_file_name)
这在ubuntu操作系统上运行得很好,但在windows中会出现错误。错误发生在最后一行'操作系统重命名(旧文件名,新文件名)。错误消息是:
^{pr2}$为什么这个脚本在不同的操作系统中表现不同? 如何让这个脚本在Windows操作系统中工作呢?在
目前没有回答
相关问题 更多 >
编程相关推荐