Change executable option after python's unzipping - python-3.x

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

Related

Where can i find the pyc file?

The python3 version is Python 3.5.3 in my os.
mkdir workspace
cd workspace
vim print.py
print("i am learning")
Saved and exit.
python3 print.py
i am learning
As far as i knew, python source file was parsed and compiled into pyc file when to execute it.
ls
print.py
There is no pyc file in workspace directory,where is the complied print.py file then?
sudo find / -name ".pyc"
The find command still can't search pyc file such as print.pyc .
python3 -m compileall can create the compiled file for print.py manually,where is the compiled file for print.py created by python itself?
Does python3 delete the print.pyc after executing python3 print.py?
Ok this is one big of a problem I ever had when I'm started to learn python few years back. Python is just like any other oop programming languages which does compilation before program execution. When python compiles its program, it creates the bite code which is you can see by standard library called dis.
import dis
print(dis.dis(your_program))
Sometimes (not always) python creates .pyc file for the running programs to improve the speed up the loading of import modules but not to improve the execution time. So hope you get intuition behind .pyc, furthermore .pyc only creates when your module is import by another module.
As an example, Imagine you have this print.py (Let's modify it shall we)
def return_print_statment(statement):
print('Printed version: ', statement)
Suppose this module imported by another custom module called views.py. In views.py there is a module_view which will use the return_print_statment
from print import return_print_statment
def module_view():
...
return_print_statment(output)
So in the compilation, since you have imported the print.py python will generate print.pyc file for it. In python 2.0 python will put the .pyc to right next to your program in the same folder, but in python3 instead of creating in the same folder python will create separate folder called __pycache__ in the same directory to put these byte codes.
python3 -m compileall .
To compile all .py files in your current directory.
http://effbot.org/pyfaq/how-do-i-create-a-pyc-file.htm

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()

How to get absolute pyc file path with python 3.x?

How can we get the compiled python pyc file path with python 3.x in the current environment? I know it's in the __pycache__ direcotry, but I couldn't find a way to find the file path. Because the names of pyc files of python 3 changes by the environment.
Given that you know the path to the source (ie .py) file, there's a function importlib.util.cache_from_source that does exactly what you want. For example, to get the .pyc file corresponding to the numpy package, you would do:
import importlib
import numpy
importlib.util.cache_from_source(numpy.__file__)
For me (on OS X), this prints out something along the lines of:
/<absolute path to site-packages>/numpy/__pycache__/__init__.cpython-36.pyc

Exe to python with pyinstaller?

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.

Resources