ubuntu the guide tarfile, win disoblige

# ### tarfile compression module suffix .tar | .tar.gz | .tar.bz2 
Import tarfile

tarfile insufficiency win in function, with kidney, stay up all night bug



# ### 1. Create a tar archive [official: theory can be suppressed to the minimum mode is bz2]
# (. 1) to create a compressed packet
"" ".tar compressed, not only the packing compression 276.48 thousand" ""
TF = tarfile.open ( "ceshi1029.tar", "W", encoding = "UTF-. 8")
# (2) the compressed file is written to the package
tf.add ( "/ bin / ntfscmp", "ntfscmp")
tf.add ( "/ bin / openvt,", "openvt,")
TF. the Add ( "/ bin / grep", "tmp / grep")
# (. 3) Close compressed
tf.close ()

# 2. Create .tar.gz archive 120 462
TF = tarfile.open ( "ceshi1030.tar.gz "," W: GZ ", encoding =" UTF-. 8 ")
tf.add (" / bin / ntfscmp "," ntfscmp ")
tf.add ("/ bin / openvt "," openvt ")
tf.add (" / bin / grep "," tmp / grep ")
tf.close ()

# 3. Create a .tar.bz2 archive 115 728
tf = tarfile.open("ceshi1031.tar.bz2","w:bz2",encoding="utf-8")
tf.add("/bin/ntfscmp","ntfscmp")
tf.add("/bin/openvt","openvt")
tf.add("/bin/grep","tmp/grep")
tf.close()

# ### 2.解压压缩包
tf = tarfile.open("ceshi1030.tar.gz","r",encoding="utf-8")
# extract(文件,路径)
# tf.extract("ntfscmp","ceshi1030")
# extractall(路径)
tf.extractall("ceshi1030")
tf.close()

# ### 3.追加文件 支持with语法
with tarfile.open("ceshi1029.tar","a",encoding="utf-8") as tf :
tf.add("/bin/gzip","gzip")

# error tarfile中的追加,Not only is packaged only additional compressed packets, other modes can not be; "" " tf.add ( "/ bin / gzip", "gzip")with tarfile.open (" ceshi1031.tar.bz2 "," A ", encoding =" UTF-. 8 ") AS TF:
"" "




4. Check # ### compressed content package
with tarfile.open ( "ceshi1029.tar", "R & lt", encoding = "UTF-. 8") AS TF:
LST = tf.getnames ()
Print (LST)


# ### tarfile how to solve the defects that exist? (not append files to already compressed package)
"" "
(1) first extract all files to a folder
(2) you want to copy the contents added to a folder
(3 ) filtered through screening, repackage compression
"" "
Import os
# get the current path of the script where
pathvar os.getcwd = ()
# Print (RES) # / mnt / hgfs / gongxiang8 / day16

# archive path
path1 = os.path .join (pathvar, "ceshi1031.tar.bz2")
Print (path1)
# decompression path
path2 = os.path.join (pathvar, "ceshi1031")

# (1) first extract all files to a folder
with tarfile.open (path1, "R & lt", encoding = "UTF-. 8") AS TF:
TF.extractall (path2)

# (2) I want to copy the contents added to a folder
= mybin "-a CP / bin / IP" -a + path2 CP # / bin / IP / mnt / hgfs / gongxiang8 / day16 / ceshi1031
the os.system (mybin)

# (. 3) was filtered through filter, repackage compression
lst = os.listdir (path2)
Print (LST)

with tarfile.open (path1, "w: bz2", encoding = "UTF-8") AS TF:
for i in LST:
! IF i = "openvt":
# splice absolute path
pathnew = the os.path.join (path2, I)
# the Add (path, alias)
tf.add (pathnew, I)























Guess you like

Origin www.cnblogs.com/max404/p/11785061.html