Python自带的tarfile模块可以方便读取tar归档文件,牛b的是可以处理使用gzip和bz2压缩归档文件tar.gz和tar.bz2。
与tarfile对应的是zipfile模块,zipfile是处理zip压缩的。请注意:os.system(cmd)可以使Python脚本执行命令,当然包括:tar -czf *.tar.gz *,tar -xzf *.tar.gz,unzip等,当我觉得这样尽管可以解决问题,但我觉得很业余。
使用tarfile压缩
import tarfile
#创建压缩包名
tar = tarfile.open("/tmp/tartest.tar.gz","w:gz")
#创建压缩包
for root,dir,files in os.walk("/tmp/tartest"):
for file in files:
fullpath = os.path.join(root,file)
tar.add(fullpath)
tar.close()
使用tarfile解压
def extract(tar_path, target_path):
try:
tar = tarfile.open(tar_path, "r:gz")
file_names = tar.getnames()
for file_name in file_names:
tar.extract(file_name, target_path)
tar.close()
except Exception, e:
raise Exception, e
其中open的原型是:
tarfile.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)
mode的值有:
'r' or 'r:*' Open for reading with transparent compression (recommended).
'r:' Open for reading exclusively without compression.
'r:gz' Open for reading with gzip compression.
'r:bz2' Open for reading with bzip2 compression.
'a' or 'a:' Open for appending with no compression. The file is created if it does not exist.
'w' or 'w:' Open for uncompressed writing.
'w:gz' Open for gzip compressed writing.
'w:bz2' Open for bzip2 compressed writing.
更多请参考:tarfile — Read and write tar archive files

PythonスクリプトがUNIXシステムで実行できない理由には、次のものが含まれます。1)CHMOD XYOUR_SCRIPT.PYを使用して実行権限を付与する不十分な権限。 2)shebangラインが正しくないか欠落している場合、#!/usr/bin/envpythonを使用する必要があります。 3)環境可変設定が誤っていない場合、OS.Environデバッグを印刷できます。 4)間違ったPythonバージョンを使用して、Shebangラインまたはコマンドラインでバージョンを指定できます。 5)仮想環境を使用して依存関係を分離する依存関係の問題。 6)構文エラー、python-mpy_compileyour_script.pyを使用して検出します。

Pythonアレイの使用は、リストよりも大量の数値データの処理に適しています。 1)配列を保存するメモリを保存します。2)アレイは数値的な値で動作するのが高速です。3)アレイフォースタイプの一貫性、4)アレイはCアレイと互換性がありますが、リストほど柔軟で便利ではありません。

listSareのより良い前提条件とmixdatatypes、whilearraysares優れたスナリカル計算砂の砂を大きくしたデータセット。

numpymanagesmemoryforlargearrayseffictificleusing biews、copies、andmemory-mappedfiles.1)rewsinging withotingcopying、directmodifying theoriginalArray.2)copiescanbecreatedwithcopy()methodforpreservingdata.3)Memory-MapplehandLemassiutasedatasetasedatasetasetasetasetasetasedas

listsinpythondonotrequireimportingamodule、whilearrays fromthearraymoduledoneedanimport.1)listsarebuiltin、versatile、andcanholdmixeddatypes.2)araysaremoremory-efficient-fornumerumerumerumerumerumerdatabutでき、対象となるンドベフェフサメタイプ。

Pythonlistscanstoreanydatatype,arraymodulearraysstoreonetype,andNumPyarraysarefornumericalcomputations.1)Listsareversatilebutlessmemory-efficient.2)Arraymodulearraysarememory-efficientforhomogeneousdata.3)NumPyarraysareoptimizedforperformanceinscient

heouttemptemptostoreavure ofthewrongdatatypeinapythonarray、yure counteractypeerror.thisduetothearraymodule'sstricttypeeencultionyを使用します

PythonListSarePartOfThestAndardarenot.liestareBuilting-in、versatile、forStoringCollectionsのpythonlistarepart。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

ドリームウィーバー CS6
ビジュアル Web 開発ツール

ホットトピック









