我使用py2exe编译了一个小的重命名程序。每当我运行可执行文件时,我收到以下错误:“第17行,在WindowsError中:[错误5]访问被拒绝”。该程序在python中断程序中运行良好,但不能作为EXE。我尝试使用管理员权限运行可执行文件,但我得到了相同的结果。下面是第17行,有谁知道为什么会这样?感谢。
for filename in filenames:
os.rename(os.path.join(path, filename), os.path.join(path, filename.replace(cur_Name, new_Name)))
答案 0 :(得分:1)
我最近在尝试删除一堆文件时遇到了这个问题,第一个是只读文件。我只是右键点击它并勾选“只读”。这解决了我的错误。
出于兴趣,这是我的计划:
#This matches (1) files and deletes them IF there is an original of the same file size.
import os
path = "e:\\"
for root, dirs, files in os.walk(path):
for lastfile in files:
if lastfile.find(' (1).') > -1:
onepos = lastfile.find(' (1).')
for thisfile in files:
matcherone = (lastfile[0:onepos] + lastfile[onepos+4:len(lastfile)])
if matcherone == thisfile:
lastfilesize = os.path.getsize(os.path.join(root,lastfile));
thisfilesize = os.path.getsize(os.path.join(root,thisfile));
if lastfilesize-thisfilesize == 0:
print "Deleting : ", os.path.join(root,lastfile);
os.remove(os.path.join(root,lastfile));