欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python3實(shí)現(xiàn)將文件樹(shù)中所有文件和子目錄歸檔到tar壓縮文件的方法

 更新時(shí)間:2015年05月22日 11:06:57   作者:work24  
這篇文章主要介紹了Python3實(shí)現(xiàn)將文件樹(shù)中所有文件和子目錄歸檔到tar壓縮文件的方法,涉及Python3使用tarfile模塊實(shí)現(xiàn)tar壓縮文件的技巧,需要的朋友可以參考下

本文實(shí)例講述了Python3實(shí)現(xiàn)將文件樹(shù)中所有文件和子目錄歸檔到tar壓縮文件的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

# 這里將一個(gè)文件樹(shù)中的所有文件和子目錄歸檔到一個(gè)tar歸檔文件,然后壓縮 
import tarfile, os 
# compression表示壓縮算法,gz表示gzip顏色,bz2表示bzip2壓縮,
# 空字符串表示不壓縮 
# folder_to_backup: 要?dú)w檔的文件夾 
# dest_folder 表示目標(biāo)文件夾 
def make_tar(folder_to_backup, dest_folder, compression = 'bz2'):
  # dest_ext 表示擴(kuò)展名 
  if compression: 
    dest_ext = '.' + compression 
  else: 
    dest_ext = '' 
  arc_name = os.path.basename(folder_to_backup) 
  # dest_name 為目標(biāo)文件名,dest_path 為目標(biāo)文件路徑 
  dest_name = '%s.tar%s' % (arc_name, dest_ext) 
  dest_path = os.path.join(dest_folder, dest_name) 
  # 壓縮方法決定了open的第二個(gè)參數(shù)是 "w", 或"w:gz", 或"w:bz2"
  if compression: 
    dest_cmp = ':' + compression 
  else: 
    dest_cmp = ''
  out = tarfile.TarFile.open(dest_path, 'w' + dest_cmp)
  out.add(folder_to_backup, arc_name)
  out.close() 
  return dest_path 
dest_path = make_tar('d:/8 file_system', 'd:/') 
print(dest_path)

希望本文所述對(duì)大家的Python3程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論