python tarfile模块基本使用

    
    
    
    
  1. # work with a tarball compressed archive file (similar to zip)
  2. # Python module tarfile is better and more flexible than zipfile
  3. # it allows gzip and the even denser bzip2 compression
  4. # tested with Python24 vegaseat 18jan2007
  5.  
  6. import tarfile
  7.  
  8. str1 = "If you want breakfast in bed, sleep in the kitchen."
  9. str2 = "Dinner is ready when the smoke alarm goes off."
  10. str3 = "How can I miss you if you won't go away?"
  11.  
  12. # write test file 1
  13. fout = open ( "TarTest1.txt", "w" )
  14. fout. write (str1 )
  15. fout. close ( )
  16.  
  17. # write test file 2
  18. fout = open ( "TarTest2.txt", "w" )
  19. fout. write (str2 )
  20. fout. close ( )
  21.  
  22. # write test file 3
  23. fout = open ( "TarTest3.txt", "w" )
  24. fout. write (str3 )
  25. fout. close ( )
  26.  
  27. # pick your compression ...
  28. # for uncompressed use file extension .tar and modifier "w"
  29. # for gzip compressed use file extension .tar.gz and modifier "w:gz"
  30. # for bzip2 super compressed use file extension .tar.bz2 and "w:bz2"
  31. # use "w", "w:gz" or "w:bz2" for all file types, including binary files
  32. tar = tarfile. open ( "TarTest.tar.bz2", "w:bz2" )
  33.  
  34. # turn the three test files into a tar archive
  35. for name in [ "TarTest1.txt", "TarTest2.txt", "TarTest3.txt" ]:
  36. tar. add (name )
  37. tar. close ( )
  38.  
  39. print '-'* 40
  40.  
  41. # test if the file is a valid tar file
  42. tfilename = "TarTest.tar.bz2"
  43. if tarfile. is_tarfile (tfilename ):
  44. print "%s is a valid tar file" % tfilename
  45. else:
  46. print "%s is not a valid tar file" % tfilename
  47.  
  48. print '-'* 40
  49.  
  50. # read the tarfile you just wrote
  51. tar = tarfile. open ( "TarTest.tar.bz2", "r:bz2" )
  52. file_list = [ ]
  53. for file in tar:
  54. # show filename and size (bytes)
  55. print "file %s has a size of %d bytes" % ( file. name, file. size )
  56. file_list. append ( file. name )
  57.  
  58. # another way to get the file list
  59. file_list2 = tar. getnames ( )
  60.  
  61. print '-'* 50
  62. print file_list
  63. print file_list2
  64. print '-'* 50
  65.  
  66. # pick one of the three files in the tar-archive (tarball)
  67. filename = file_list [ 1 ]
  68.  
  69. # decompress the particular file
  70. data = tar. extractfile (filename ). read ( )
  71.  
  72. # if it's a text file, show contents
  73. if filename. endswith ( ".txt" ):
  74. print "Content of file %s in the tarball:" % filename
  75. print data
  76.  
  77. # write the files out to new files
  78. for fname in tar. getnames ( ):
  79. # extract/decompress the data of each file
  80. data = tar. extractfile (fname ). read ( )
  81. # optionally change the filename
  82. new_file = "new_" + fname
  83. print "File %s written!" % new_file
  84. # write the decompressed data to a file
  85. fout = open (new_file, "w" )
  86. fout. write (data )
  87. fout. close ( )
  88.  
  89. # done, close the tar file ...
  90. tar. close ( )

 

    
    
    
    
  1. # work with a tarball compressed archive file (similar to zip)
  2. # Python module tarfile is better and more flexible than zipfile
  3. # it allows gzip and the even denser bzip2 compression
  4. # tested with Python24 vegaseat 18jan2007
  5.  
  6. import tarfile
  7.  
  8. str1 = "If you want breakfast in bed, sleep in the kitchen."
  9. str2 = "Dinner is ready when the smoke alarm goes off."
  10. str3 = "How can I miss you if you won't go away?"
  11.  
  12. # write test file 1
  13. fout = open ( "TarTest1.txt", "w" )
  14. fout. write (str1 )
  15. fout. close ( )
  16.  
  17. # write test file 2
  18. fout = open ( "TarTest2.txt", "w" )
  19. fout. write (str2 )
  20. fout. close ( )
  21.  
  22. # write test file 3
  23. fout = open ( "TarTest3.txt", "w" )
  24. fout. write (str3 )
  25. fout. close ( )
  26.  
  27. # pick your compression ...
  28. # for uncompressed use file extension .tar and modifier "w"
  29. # for gzip compressed use file extension .tar.gz and modifier "w:gz"
  30. # for bzip2 super compressed use file extension .tar.bz2 and "w:bz2"
  31. # use "w", "w:gz" or "w:bz2" for all file types, including binary files
  32. tar = tarfile. open ( "TarTest.tar.bz2", "w:bz2" )
  33.  
  34. # turn the three test files into a tar archive
  35. for name in [ "TarTest1.txt", "TarTest2.txt", "TarTest3.txt" ]:
  36. tar. add (name )
  37. tar. close ( )
  38.  
  39. print '-'* 40
  40.  
  41. # test if the file is a valid tar file
  42. tfilename = "TarTest.tar.bz2"
  43. if tarfile. is_tarfile (tfilename ):
  44. print "%s is a valid tar file" % tfilename
  45. else:
  46. print "%s is not a valid tar file" % tfilename
  47.  
  48. print '-'* 40
  49.  
  50. # read the tarfile you just wrote
  51. tar = tarfile. open ( "TarTest.tar.bz2", "r:bz2" )
  52. file_list = [ ]
  53. for file in tar:
  54. # show filename and size (bytes)
  55. print "file %s has a size of %d bytes" % ( file. name, file. size )
  56. file_list. append ( file. name )
  57.  
  58. # another way to get the file list
  59. file_list2 = tar. getnames ( )
  60.  
  61. print '-'* 50
  62. print file_list
  63. print file_list2
  64. print '-'* 50
  65.  
  66. # pick one of the three files in the tar-archive (tarball)
  67. filename = file_list [ 1 ]
  68.  
  69. # decompress the particular file
  70. data = tar. extractfile (filename ). read ( )
  71.  
  72. # if it's a text file, show contents
  73. if filename. endswith ( ".txt" ):
  74. print "Content of file %s in the tarball:" % filename
  75. print data
  76.  
  77. # write the files out to new files
  78. for fname in tar. getnames ( ):
  79. # extract/decompress the data of each file
  80. data = tar. extractfile (fname ). read ( )
  81. # optionally change the filename
  82. new_file = "new_" + fname
  83. print "File %s written!" % new_file
  84. # write the decompressed data to a file
  85. fout = open (new_file, "w" )
  86. fout. write (data )
  87. fout. close ( )
  88.  
  89. # done, close the tar file ...
  90. tar. close ( )
# work with a tarball compressed archive file (similar to zip) # Python module tarfile is better and more flexible than zipfile # it allows gzip and the even denser bzip2 compression # tested with Python24 vegaseat 18jan2007 import tarfile str1 = "If you want breakfast in bed, sleep in the kitchen." str2 = "Dinner is ready when the smoke alarm goes off." str3 = "How can I miss you if you won't go away?" # write test file 1 fout = open("TarTest1.txt", "w") fout.write(str1) fout.close() # write test file 2 fout = open("TarTest2.txt", "w") fout.write(str2) fout.close() # write test file 3 fout = open("TarTest3.txt", "w") fout.write(str3) fout.close() # pick your compression ... # for uncompressed use file extension .tar and modifier "w" # for gzip compressed use file extension .tar.gz and modifier "w:gz" # for bzip2 super compressed use file extension .tar.bz2 and "w:bz2" # use "w", "w:gz" or "w:bz2" for all file types, including binary files tar = tarfile.open("TarTest.tar.bz2", "w:bz2") # turn the three test files into a tar archive for name in ["TarTest1.txt", "TarTest2.txt", "TarTest3.txt"]: tar.add(name) tar.close() print '-'*40 # test if the file is a valid tar file tfilename = "TarTest.tar.bz2" if tarfile.is_tarfile(tfilename): print "%s is a valid tar file" % tfilename else: print "%s is not a valid tar file" % tfilename print '-'*40 # read the tarfile you just wrote tar = tarfile.open("TarTest.tar.bz2", "r:bz2") file_list = [] for file in tar: # show filename and size (bytes) print "file %s has a size of %d bytes" % (file.name, file.size) file_list.append(file.name) # another way to get the file list file_list2 = tar.getnames() print '-'*50 print file_list print file_list2 print '-'*50 # pick one of the three files in the tar-archive (tarball) filename = file_list[1] # decompress the particular file data = tar.extractfile(filename).read() # if it's a text file, show contents if filename.endswith(".txt"): print "Content of file %s in the tarball:" % filename print data # write the files out to new files for fname in tar.getnames(): # extract/decompress the data of each file data = tar.extractfile(fname).read() # optionally change the filename new_file = "new_" + fname print "File %s written!" % new_file # write the decompressed data to a file fout = open(new_file, "w") fout.write(data) fout.close() # done, close the tar file ... tar.close()

你可能感兴趣的:(压缩,python,python,职场,休闲,pyton,tarfile模块)