# -*- coding: utf-8 -*-
"""
Created on Tue Nov 27 10:52:57 2018
"""
Created on Tue Nov 27 10:52:57 2018
@author: issta
"""
"""
import os
import tarfile
__author__ = 'Administrator'
import tarfile
__author__ = 'Administrator'
RelativeOrAbsoluteTar = True #False
def archive():
cwd = os.getcwd()
tar = tarfile.open('IsstaGao'+".tar.gz", 'w:gz')
for root, dirs, files in os.walk(cwd):
for file in files:
fullpath = os.path.join(root,file)
if RelativeOrAbsoluteTar :
used_path = os.path.relpath(fullpath, start=os.curdir)
#relative_path = os.path.relpath(fullpath, start = cwd)
else :
used_path = fullpath
tar.add(used_path)
tar.close()
if __name__=='__main__':
archive()
def archive():
cwd = os.getcwd()
tar = tarfile.open('IsstaGao'+".tar.gz", 'w:gz')
for root, dirs, files in os.walk(cwd):
for file in files:
fullpath = os.path.join(root,file)
if RelativeOrAbsoluteTar :
used_path = os.path.relpath(fullpath, start=os.curdir)
#relative_path = os.path.relpath(fullpath, start = cwd)
else :
used_path = fullpath
tar.add(used_path)
tar.close()
if __name__=='__main__':
archive()
------------------运行结果:
本文介绍了一个使用Python实现的自动化归档脚本,该脚本利用tarfile模块来压缩当前工作目录下的所有文件和子目录。用户可以选择是否使用相对路径进行打包,这为备份和归档提供了灵活性。

1274

被折叠的 条评论
为什么被折叠?



