How to read/extract contents of .tar.7z file using python - python-3.x

I have a .tar.7z file which I want to unpack (when I unpack with archive_utility.app on MAC manually, I am getting a folder with subfolder and files).
I am not able to unpack this .tar.7z file with Python. Please let me know which module will let me do so.
i tried this using tarfile like below and failed to open it.
import tarfile
# open file
# file = tarfile.open('ckrseoselr7202-logs.tar.gz') # Working
file = tarfile.open('ckrseoselr7202-logs.tar.7z') # Not Working
# extracting file
file.extractall('./Folder')
file.close()
Same way i need to open .tar.7z file and want to read one particular file to parse the data. Let me know how i can achieve this using python3.

Related

How do I import a file to execute a function

I have a file with a function. I want to import the file and when doing that I get No module named ex25.
import ex25
I have checked some tutorials and the offial documentation.
import ex25
No module named ex25
First, check if the file is .py file and in the same folder with the file that you are currently working on, there shouldn't be any problem at all.
If not in the same folder, but in a different folder and make sure that your file is .py file, you need to make that folder (which contains the file) to a package by adding a file name __init__.py

A batch file will not run when called from Python

I'm trying to automate some file processing that occurs when you do data analysis on fastq files using STRaitRazor. Briefly, the files themselves are gzipped and the batch file looks into these contents, pulls out necessary information and then stores it in a new folder it creates. This works well manually but when I tried to use Python to call the batch file, the window opens for a split second and then disappears without running.
I've included the basic code I'm using here. There are no error messages but the batch file does not run. I've tried substituting "call" with "Popen" and "run" in the syntax and that also has not worked.
import subprocess
batch = "C:\\STRaitRazor\\Analysis\\fastq\\Data_2\\batchCstr8.bat"
subprocess.call([r"%s"%batch])
First off, I'd recommend calling this script from the command line to see what errors it outputs. But, as a guess, I'm assuming the batch file fails because it's not being run from the directory it expects to be run from, you'll want to change to it's directory first:
import subprocess
import os
batch = "C:\\STRaitRazor\\Analysis\\fastq\\Data_2\\batchCstr8.bat"
# Get the current directory, and change to the directory that batch file is in
previous_dir = os.getcwd()
os.chdir(os.path.dirname(batch))
# Run the batch file (note: don't need the % operator here, also, use check_call
# to raise an exception if calling the batch file fails)
subprocess.check_call([batch])
# Change back to the old working directory, not necessary, but helpful
# if there are any other steps to run after the batch file is done
os.chdir(previous_dir)

How to extract a zip file using python 3

How to extract a zip file using python when zip file present in different directory where script file present.
I try this ,but i got error because source path is not accepted ,try to solve me this problem.
from zipfile import ZipFile
def func(source, target):
with ZipFile('source', 'target'):
ZipFile.Extractall('target')
Use this code. To move through directories you could either hard code the directory where your script is present or you could toggle through the directories using simple commands such as "../" to move out of the given directory or "/" to move inside a folder in the directory. For example - "../script.py" or "/folder/script.py". Similarly you can use this to find your .zip file.
import zipfile
with zipfile.ZipFile("file.zip","r") as zip_ref:
zip_ref.extractall("targetdir")
For just unpacking, shutil should suffice:
import shutil
shutil.unpack_archive('path-to-zipfile')
You'll have to check for source path of the zip file which is relative to your current working directory. To know your current working directory you can try
import os
print(os.getcwd())
zip - Unzipping files in python
relative-paths-in-python

Handling winrar archive files in python

I'm using Python 3.6 and I want to know if there is a way to handle winrar (.Z extension) files in Python. I have used the following code:
zip_ref = zipfile.ZipFile('POTCAR.z','r') #POTCAR.z is the winrar archive file
zip_ref.extract('folder to which I want to write')
zip_ref.close()
But I keep getting this error:
BadZipFile: File is not a zip file
What is the solution to this? Is there any other library for handling such files?
I remember that I have used zLib for extracting winrar archives.
You could use gzip:
gzip_file = gzip.open('Potcar.z') # use gzip.open instead of builtin open function
file_content = gzip_file.read()
and to save it (if it is a text file - since you didnt specify):
file = open(“my_file.txt”,”w”)
file.write(file_content)
file.close()

Rename a file without changing file extension

I have downloaded a csv file(ABC) using python and selenium and need to change the name of the file.
Input= ABC.csv #downloaded file with name ABC
Output= DEF.csv #New file with name DEF.
Any help is appreciated.
try this
import os
os.rename('a.txt', 'b.kml')
From:
How to rename a file using Python
Thanks All.. Used the below code and it is working fine. All others kept throwing the error as File not found. Used complete file path.
os.rename('C:\\Users\\pathname\\ABC.csv', 'C:\\Users\\pathname\\DEF.csv')
You can use os module to rename the file:
import os
os.rename('path to ABC.csv', 'path to DEF.csv')
Explanation:
rename first argument is the original file which needs to be renamed and second argument is the new name which it should be renamed. If the files are in current directory it will work by simply using the file names. Otherwise include the path where these files are there
More information at docs **https://docs.python.org/3/library/os.html#os.rename

Resources