我成功地创建了一个脚本,它将目录中的所有$都更改为#,但它不影响文件夹名称。所以文件夹名仍然保持相同的名称。我如何修改这个以更改文件夹名称中的$到#以及?在Python中重命名文件夹
def cleanFiles(self):
directoryChosen = self.directoryChoice()
print directoryChosen + " you made it to files selected"
#for file_names in os.listdir(directoryChosen):
#self.listWidget.addItem(file_names)
for n in os.listdir(directoryChosen):
print n + " made it here"
self.listWidget.addItem(n)
if os.path.isdir(directoryChosen):
print directoryChosen + " almost there"
newname = n.replace('$', '#')
print newname + " this is newname"
if newname != n:
print newname
print n
path = os.path.join(directoryChosen + '/' + n)
print path
target = os.path.join(directoryChosen + '/' + newname)
print target
os.rename(path, target)
UnboundLocalError:在赋值之前引用的局部变量'n' – Anekdotin
@Eddwinn好抓! :)这是一个编辑失败。再试一次。 – idjaw
在会议上展示这一点。稍后更新 – Anekdotin