我是使用Python进行简单管理任务的初学者python程序员。
我写了一个小的日志移动代码(因为我有一个系统,该系统会生成许多从服务器移动到存储的日志文件)。
我在远程路径上的文件上遇到权限错误,尝试复制时权限被拒绝,因此将其包装在try:语句中以尝试并通过该错误,但脚本仍在结束...第32行已关闭。 copy2(位置,copylocation)。
基本路径是本地目录,日志目录是UNC路径(随文件夹集追加)。文件夹在脚本前面的两个文件系统中均已镜像。 (因此应始终存在)。
其他尝试例如在os.remove周围也可以。
如果有人能指出我的一般方向,将不胜感激。
Traceback (most recent call last):
File "C:\Program Files (x86)\Log Mover\log_mover.py", line 32,
shutil.copy2 (location,copylocation)
File "C:\Python27\lib\shutil.py", line 127, in copy2
copyfile(src, dst)
File "C:\Python27\lib\shutil.py", line 82, in copyfile
with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: '\\\\XXXX\\d$\\logs\\
logs/XXX\\XXX - 2012-07-03 21-49.XXXX'
☀could not delete:C:/logs/XX\XXX\XXX 2012-07-04 08-00-42.txt
Traceback (most recent call last):
for root, dirs, files in os.walk(basepath):
for filenames in files:
location = os.path.join(root, filenames)
try:
datemod = time.strftime("%Y%m%d%H",time.localtime(os.path.getmtime(location)))
except OSError:
print "count not get mod time:"+location
if datemod != currtime:
drive,path = os.path.splitdrive(location)
copylocation = str(logdir)+str(path)
try:
shutil.copy2 (location,copylocation)
except OSError:
print "could not copy"
pass
if os.path.getsize(location) == os.path.getsize(copylocation):
try:
os.remove(location)
print "Deleted"+location
except OSError:
print "could not delete:"+location
pass
else:
print "sizes dont match"
谢谢
肯尼
最佳答案
当您遇到IOError时,您正在尝试捕获OSError。
添加此:
except (OSError, IOError):
#some code