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()
Related
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.
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
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
I have a small trouble with file option - executable, after using extractall.
This is my part of code:
import zipfile
archive = zipfile.ZipFile(path_to_local_folder+file, 'r')
archive.extractall(path_to_local_folder)
os.remove(path_to_local_folder+file)
In my zip archive I have a file, which should have an option - executable. Of course I can add operation "make file as executable", but I think it's not a good idea.
P.S. I use Mac OS and Python 3.5
So I made a huge mistake and deleted my code file (python). The only thing I have is my python file as .exe that I created with pyinstaller. Is there a way to reverse this and to extract my code file from .exe?
You can extract the contents of the .exe file using PyInstaller Extractor. Run it like this:
python pyinstxtractor.py executable.exe
You will then get a bunch of files, including your original python file.