Uncompress an tar.bz2 file with Python tarfile module
You use tar.extractall with wrong argument. I think, you need something like this
import tarfile
tar = tarfile.open("path_to/test/sample.tar.bz2", "r:bz2")
tar.extractall()
tar.close()
or
import tarfile
tar = tarfile.open("path_to/test/sample.tar.bz2", "r:bz2")
for i in tar:
tar.extractfile(i)
tar.close()
If you need to extract files to some specific folder
import tarfile
tar = tarfile.open("path_to/test/sample.tar.bz2", "r:bz2")
tar.extractall(some_path)
tar.close()
How to untar a file
The message is saying that, in your current directory (which I assume is your home directory, when you start your bash shell in default manner), there is no file such as lammps-stable.tar.gz.
To solve this, I will break the problem in two steps.
(1) First find where is the "lammps-stable.tar.gz"
(2) Use the full path (obtained in step 1) and give it to tar command
How to get these.
Folder structure in Ubuntu starts from "/" (also called root). All other folders are nested within it. Every user has a folder in "/home". Say the username is "singh", all the folders and files by default will be stored in "/home/singh" or somewhere nested within it. You can know your user name by typing "whoami" in the bash shell or to locate your current directory, you can type "pwd".
(1) If you have a fair idea where the "lammps-stable.tar.gz" might be then the search will be shortened otherwise it will take a long time to finish (alongwith many warnings of 'Permission Denied').
(1-a) You have rough idea that the "lammps-stable.tar.gz" is in /home/singh/Documents, then you will say the following
find "/home/singh/Document" -name "lammps-stable.tar.gz"
See the output of above command and let us call it step 1 result.
(1-b) You don't have much idea, where it is but you are sure the file belongs to you (user = singh), then I would say the following. This might take long time.
find "/home/singh/" -name "lammps-stable.tar.gz"
See the output of above command and let us call it step 1 result
(1-c) You don't any idea, where it is but you are sure the file is somewhere on the computer, then I would say the following. This might take long time.
find "/" -name "lammps-stable.tar.gz"
See the output of above command and let us call it step 1 result
(2) Now using the result of Step1, just copy the path (let's say it is /MyFullPathToTheTarGZfile), then do the following.
tar xvzf /MyFullPathToTheTarGZfile
All the best.
Extracting a .tar file returns, “This does not look like a tar archive.”
It's not a tar file. The download fails and the ".tar" file you get is really a html fail saying:
"
403 ForbiddenForbidden
You don't have permission to access /public/snake_toxins/SNAKE_ALL.tar
on this server.
Apache/2.2.15 (Linux/SUSE) mod_ssl/2.2.15 OpenSSL/1.0.0 PHP/5.3.3 Server at www.cbs.dtu.dk Port 80
"
Next time you get something like this run the commman:
file SNAKE_ALL.tar to see what kind of file it really is.
Reading individual bz2 files from a tar file
BZ2File
expects a file name as first argument and you pass a file object (i.e. an object which has the same API as what Python returns for open()
).
To do what you want, you'll have to read all the bytes from t_extract
yourself and call bz2.decompress(data)
or use BZ2Decompressor
to stream the data through it.
Extracting .tar file isn't working
try the following command:
tar -xzvf filename.tar.gz
Untar file with subprocess.call is running successfully but with no effect
If you read the man page, you'll see that the -C
parameter is sensitive to order -- it only affects operations that come after it on the command line. So, your file is being unzipped into whatever random directory you happen to be in.
You don't need shell for this. Do:
import os
import subprocess
os.chdir('/folder')
subprocess.call( ['tar','xvfz', 'file.tar.gz'] )
Related Topics
How to Close a Netcat Connection After a Certain Character Is Returned in the Response
How to Write Linux Driver Module Call/Use Another Driver Module
How to List the Contents of a Package Using Yum
How to Create Threads Without System Calls in Linux X86 Gas Assembly
Error with Gradlew: /Usr/Bin/Env: Bash: No Such File or Directory
Linux Serial Port Listener and Interpreter
Linux Debian Crontab Job Not Executed
How to Install Packages from Command Line on Suse
Awk One Liner Select Only Rows Based on Value of a Column
Linux Bash: Move Multiple Different Files into Same Directory
How to Calculate an Md5 Checksum of a Directory
What Is Raw Socket in Socket Programming
Output File Lines from Last to First in Bash
Ld: Using -Rpath,$Origin Inside a Shared Library (Recursive)
How to Get the Physical Address from the Logical One in a Linux Kernel Module