我应该压缩这个名为“ log”的目录,其中包含来自不同设备的各种日志(近50个日志)。到目前为止,这是我尝试过的代码...
import tarfile
import os
import glob
def value():
tar = tarfile.open("Archive.tgz","w:gz")
path = '/var/log'
cwd = os.getcwd()
for name in glob.glob(path):
tar.add(name, arcname = "log")
tar.close()
if os.path.isfile('./Archive.tgz'):
print "Compressed Successfully !"
else:
print "File not Found!"
value()
我仍然收到此错误:
File "tar2.py", line 18, in <module>
value()
File "tar2.py", line 11, in value
tar.add(name, arcname = "log")
File "../../../../../../../src/dist/python/Lib/tarfile.py", line 2002, in add
File "../../../../../../../src/dist/python/Lib/tarfile.py", line 1994, in add
IOError: [Errno 13] Permission denied: '/var/log/cscript.log'
我在线阅读了很多有关此错误的信息,并尝试使用chmod。但是什么都行不通。 请帮帮我!
答案 0 :(得分:0)
此错误(EACCES
)是从基础操作系统(文件系统)传播的,您可以用脚本对此做很多事情。您需要向运行脚本的用户授予(读取)权限。或者,您可以与对该文件拥有足够权限的其他用户一起运行脚本。
您提到更改了文件的模式,但是是否仍未授予对脚本运行所在的有效用户ID的访问权限?您可以通过在外壳程序中运行id
来验证当前用户ID及其所属的组。例如,文件的长列表将告诉您当前的模式设置(ls -l /var/log/cscript.log
)。
它也可能发生。即使您更改权限位,写入此文件的过程也可以检查并重置,或者使用原始模式/所有权信息重新创建文件。并且写入它的过程已经重新开始。