How do I open/convert .pkz files? - python

A python package that I'm using has data stored under a single file with a .pkz extension. How would I unzip (?) this file to view the format of data within?

Looks like what you are referencing is just a one-off file format used in sample data in scikit-learn. The .pkz is just a compressed version of a Python pickle file which usually has the extension .pkl.
Specifically you can see this in one of their sample files here along with the fact they are using the zlib_codec. To open it, you can go in reverse or try uncompressing from the command line.

Before attempting to open an PKZ file, you'll need to determine what kind of file you are dealing with and whether it is even possible to open or view the file format.
Files which are given the .PKZ extension are known as Winoncd Images Mask files, however other file types may also use this extension. If you are aware of any additional file formats that use the PKZ extension, please let us know.
How to open a PKZ file:
The best way to open an PKZ file is to simply double-click it and let the default assoisated application open the file. If you are unable to open the file this way, it may be because you do not have the correct application associated with the extension to view or edit the PKZ file.
If you can do it, great, you have a program installed that can do it, lets say that program is called pkzexecutor.exe, with python, you just have to do:
import subprocess
import os
path_to_notepad = 'C:\\Windows\\System32\\pkzexecutor.exe'
path_to_file = 'C:\\Users\\Desktop\\yourfile.pkz'
subprocess.call([path_to_notepad, path_to_file])

From the source code for fetch_olivetti_faces, the file appears to be downloaded from http://cs.nyu.edu/~roweis/data/ and originally has a .mat file extension, meaning it is actually a MATLAB file. If you have access to MATLAB or another program which can read those files, try opening it from there with the original file extension and see what that gives you.
(If you want to try opening this file in Python itself, then perhaps give this question a look: Read .mat files in Python )

Related

Target a file and copy to another directory

I am working on a bot that downloads a file using python and selenium webdriver. I have successfully been able to design the bot and it is working perfectly. The issue is
I want to target this file that was just downloaded. This program is going to be used by many people who may have set different directory for downloads. How do I use the OS module to achieve this?
Once the data is downloaded from the site, the default name is transactions; my users may have other files with the same name and my program might copy a wrong file. The file is in an excel format.
How do I target this file alone and not any other file even if they have the same name or is there a way I can change the directory my bot save any file it downloads so I don't need to copy to the directory where I want it for calculations?
consider use of shutil
newfilename = os.path.join(os.path.abspath('foopath'), 'foofile') shutil.copyfile('transactions', newfilename)
Reference other options at
copy-a-file-from-one-location-to-another-in-python

Is there a way to embed an .mp3 file into a .py file?

I want to make a python application with a sound being executed when pressing a button and I also want users to download it from somewhere. But, I have to add an mp3 file to play the sound. The mp3 file must be in the same directory as the .py file. But when the user downloads it, the directory changes according to his PC. So, how will I make it so that the mp3 file is in the same directory as the .py file to recognize it and play it? Is there any way to completely add the mp3 file in the .py file without it being seperated? If yes how? Thanks!
To address this problem (including data in a Python script) I wrote py-include.
The core of that program is basically this:
with open(path, "rb") as img:
data = img.read()
data = base64.b85encode(data).decode("ascii")
It encodes the binary file contents using base85 encoding and converts it to text.
An example:
python src/scripts/public/py-include.py /usr/local/lib/firefox/browser/chrome/icons/default/default16.png
# /usr/local/lib/firefox/browser/chrome/icons/default/default16.png
data = base64.b85decode(
'iBL{Q4GJ0x0000DNk~Le0000G0000G2nGNE03Y-JVE_OEnMp)JRCoc5j04PNM-<0#Gjs9$KCit'
'&ZQHhO8(|qWsu|UG8`qAjU~S`j_PclH^ww_oBu9U-B_*}v9!PrmKXLkh&6|$A5hphyGE>Hndf;'
'CJ<dw-g%3ITC^4IG0?jnl8x}#bsS$o)pf20+F+%s`UhT~7jnVaa%-GMP`>e#RDmTz`D5Z#$t3P'
'K+MVh4l|;`z_2$?txr0Oa=R%PDkclJTkpg9Dvt>Vxk;`vjwNR-KrgvS#MMwhNfC&*xw?sHB|-z'
'P|!xcQSJ2))xX7q6JJpMWzASXwOP%=RKQicNplbo=tVdRJ{CViX*GQ8IYtM18BQY(+W<Y9Y7wK'
'n1X>ke!9gz=UmS&m)(k+SfOKw$WJeUZ=m6z30NTF1nsKXnMnv#LkI-Y28Ii)>L1AWt50XOoyE8'
'<^1*#qL$?S07%B?74(WLVUOZmhG4Nwp0c;BC93(vO)Wg)gtqFrH*}#$NIcl4WP;Wyg4J89Zb*K'
'#BY0Iayf&&r2YQ|TlSh7iAWCOm=k#j!zeoTQ)u+c)(M}jVt6*LXzMtk6a>3m~HM1#{5b)EHj!;'
'<yGM7E1<H<5as6SjMnNve$W0#Oeosb!#<{?y#)4yu8>F!X!iG}J<NEzD=k-wdrT6Wkik9(|K-('
'+dGmfrZEruNgtb{!I#CUGch42B)n%FwC#3eguI{ItI#rZNW&=4pIsVhHg7CtHL{}r(QhC1CYo+'
'X?*3RPqL$>9SZ|3bQGbwzXl|&e{8{troC_obn!cHl#2iq0KnSpO##EFZ%f!dFk5U9r22$mgwl>'
'EQuMnay;>0+Wj^(Da?-%6$Etq{_(RZzNzetN?17>jpwQx>op~qO?#ns}u$~3T>H$)`AslTIpBD'
'fC002ovPDHLkV1f'
)
Include the output from this script in your Python file, and data will contain the (binary) contents of the icon as bytes, which you can then e.g. save to a temporary file.
Note that the data will become ≈1.35 times as big because of the encoding.
py-include has an option to compress files before encoding, but since an MP3 file is already heavily compressed that doesn't yield a size reduction in this case.
A possible solution to package your application + dependecies is to create an executable from them.
I personally use pyInstaller and py2exe, but these answers cover the topic more in detail.
How can I make a Python script standalone executable to run without ANY dependency?
Create a single executable from a Python project

python Tkinter tkFileDialog

in short, what's the difference between
tkFileDialog.asksaveasfile
and
tkFileDialog.asksaveasfilename
I could not understand from the build in docs
asksaveasfile asks the user for a file, then opens that file in write mode and returns it to you so you can write in it.
asksaveasfilename asks the user for a file, then returns that file's name. No file is opened; if you want to write to the file, you'll have to open it yourself.
asksaveasfilename might be preferred over asksaveasfile if you want to do something fancier to the file than just writing data to it. For instance, you might want to first copy the file to another directory as a backup. In which case, you'd prefer to get just the file name so you can perform the copy without having to worry about whether having the file open will cause the copy to fail.
According to the http://tkinter.unpythonic.net/ wiki:
Similar to:
First you have to decide if you want to open a file or just want to get a filename in order to open the file on your own. In the first case you should use tkFileDialog.askopenfile() in the latter case tkFileDialog.askopenfilename().
then:
Saving files works in a similar way. You also have two variants of the function, one to get an opened file which you can use to save your data and another to get a file name in order to open the file on your own. These functions are only provided in the single file version. A multiple file version would make no sense.

Reading from a CSV file while it is being written to

So before I start I know this is not the proper way to go about doing this but this is the only method that I have for accessing the data I need on the fly.
I have a system which is writing telemetry data to a .csv file while it is running. I need to see some of this data while it is being written but it is not being broadcast in a manner which allows me to do this.
Question: How do I read from a CSV file which is being written to safely.
Typically I would open the file and look at the values but I am hoping to be able to write a python script which is able to examine the csv for me and report the most recent values written without compromising the systems ability to write to the file.
I have absolutely NO access to the system or the manner in which it is writing to the CSV I am only able to see that the CSV file is being updated as the system runs.
Again I know this is NOT the right way to do this but any help you could provide would be extremely helpful.
This is mostly being run in a Windows environment
You can do something like:
tailf csv_file | python your_script.py
and read from sys.stdin

can linux command line programs see python temporary files?

I have a simple web-server written using Python Twisted. Users can log in and use it to generate certain reports (pdf-format), specific to that user. The report is made by having a .tex template file where I replace certain content depending on user, including embedding user-specific graphs (.png or similar), then use the command line program pdflatex to generate the pdf.
Currently the graphs are saved in a tmp folder, and that path is then put into the .tex template before calling pdflatex. But this probably opens up a whole pile of problems when the number of users increases, so I want to use temporary files (tempfile module) instead of a real tmp folder. Is there any way I can make pdflatex see these temporary files? Or am I doing this the wrong way?
without any code it's hard to tell you how, but
Is there any way I can make pdflatex see these temporary files?
yes you can print the path to the temporary file by using a named temporary file:
>>> with tempfile.NamedTemporaryFile() as temp:
... print temp.name
...
/tmp/tmp7gjBHU
As commented you can use tempfile.NamedTemporaryFile. The problem is that this will be deleted once it is closed. That means you have to run pdflatex while the file is still referenced within python.
As an alternative way you could just save the picture with a randomly generated name. The tempfile is designed to allow you to create temporary files on various platforms in a consistent way. This is not what you need, since you'll always run the script on the same webserver I guess.
You could generate random file names using the uuid module:
import uuid
for i in xrange(3):
print(str(uuid.uuid4()))
The you save the pictures explictly using the random name and pass insert it into the tex-file.
After running pdflatex you explicitly have to delete the file, which is the drawback of that approach.

Categories