File extension validation with python [duplicate] - python

This question already has answers here:
How to check type of files without extensions? [duplicate]
(10 answers)
Closed 9 years ago.
I want to check if a given file's extension is correct or not. For example, someone give me a file with an extension .zip but actually it may be an executable.
Using mimetypes I could not determine a file's real type. As far as I see, mimetypes needs an extension.
I can map the output of unix file command with some extensions. Even if you change the extension, you cannot deceive file command. However, this solution needs a subprocess.
I thought, there may be a more pythonic solution of this problem. Does anyone know?

Searching the name of the C library (libmagic) used for the file command, nets 3 interesting python packages on PyPI:
libmagic (bitbucket repo)
python-magic (you can find some documentation in the github repo)
filemagic (github repo)

Ultimately, there is no absolute way of knowing. For several reasons:
Some file format use simple identifiers, but others don't.
For those that don't, the only way is analyzing the behavior of a program able to able the format. If the program can successfully open the file, then it belongs to it.
But if not, the file could belong to hundreds of formats you don't have a program to open with.
I'm afraid you will need to be content with a partial answer like the ones you already have.

Related

Is there a way to run Python code without downloading Python? [duplicate]

This question already has answers here:
How can I convert a .py to .exe for Python?
(8 answers)
Closed 1 year ago.
My internship has asked me to make my code executable for people who do not have Python downloaded. I can run parts of it on online Python compilers, but it uses different libraries that are not supported on those compilers. My program opens a .csv file specified by the user and filters it and writes the new data to a new .csv file. To implement the code online, I think I would need to learn JavaScript? Is there any other alternative? I appreciate any guidance provided!
You can create a standalone executable that includes your python script + all necessary files to run it. The users then just run ".exe" file without need to download and setup python environment. A great library to do it is cx-freeze
There is many online python runner. Example https://www.programiz.com/python-programming/online-compiler/

Python built-in module for handling Excel files

I know similar questions have been popular in the past, but none refers to my problem. I'm looking for a way to read data from Excel file in Python, but I'm strongly against using non-builtin modules.
The reason why is that in my case Python is a component of another software, so incorporating additional module would require from every user knowledge about how to use pip, which Python installation on your pc should one install module into, etc. The solution must not require any additional actions from user.
I can read CSV files with Python builtin easily, so that could work, but how can I convert Excel to CSV in the first place? Or is there a way to read Excel directly?
Edit: It is Python 2, that is used in this software.
Edit2:
Anyone minds explaining the downvote? I think this isn't a question about a ready solution or module, but rather a method and is well detailed. It is not always possible to use external modules, so this is an actual problem. If it is not possible at all though, then I would simply expect an answer instead of -1.
Not really the prettiest solution, but you could download the complete code repository of one of the excel handling packages for python (openpyxl for example) and put these files in the same directory as the python script that you're going to run. Subsequently you can do an import of these local package files in your script.
Note: if the excel handling package has dependencies on other packages, then you'll need to download these as well.

Hide/protect Python code [duplicate]

This question already has answers here:
How do I protect Python code from being read by users?
(29 answers)
Closed 9 years ago.
I am writing code (Python and wxpython for GUI) which will run on Debian OS on Raspberry PI. I want to protect/hide the source code. Is there any way to do it? Probably py2exe, or converting it to a library or something else?
The compiled code (.pyc files) can be used if you wish for others to be able to execute but not to read or modify the source code (.py, .pyw).
Simply:
run your application
then copy all the relevant .pyc files into another folder and you should be able to
run it all from the new location
So long as all the appropriate modules are still able to be loaded, everything will work. This will require the version of python to be the same (can't run .pyc files from python 2.4 with python 2.7 and vice-versa)
The other thing to know is that strings will be preserved. You should open them up in a good text editor (I use vim) and inspect the content if you are worried about what others can see.
py2exe is of course another example, but you lose the ability to have cross-platform code at that point -- and if your application is for the Raspberry Pi -- that won't work.
Since you provided no other information about how you intend to run the code, it's not clear if the source will be a module or intended to be run directly. You should read this post to learn more.

how to tranfrom python3 program into exe file totally? [duplicate]

This question already has answers here:
Python 3 project into exe?
(3 answers)
Closed 9 years ago.
I have heard py2exe,but it doesn't support python3 now.So I found cxfreeze,but there is an problem that the extension lib are in a zip file,but one extension must use a txt file.The extension would figure out the txt file address,which in the zip file.Windows throw out an error of 'FileNotFoundError'.The problem nearly drive me mad.It can't open file that compressed in zip.I am begging for your help...
Reposting as an answer:
Programs that load data files may need to be modified a bit if they're looking for the data files adjacent to Python modules, because cx_Freeze puts the Python modules into a zip file. Depending on your use case, you could load the data from the zip file (using the zipfile module), or load it from a regular file alongside the exe. The cx_Freeze FAQ has an example of how to do the latter.
Another option, especially for small pieces of data, is to embed it in Python code so it's in a frozen module. Qt's resource system works like this.

Gather all Python modules used into one folder? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Gather all Python modules used into one folder?
I don't think this has been asked before-I have a folder that has lots of different .py files. The script I've made only uses some-but some call others & I don't know all the ones being used. Is there a program that will get everything needed to make that script run into one folder?
Cheers!
Since Python is not statically linked language, this task would be rather a challenging one. Especially if some of your code uses eval(...) or exec(...).
If your script is not very big, I would just move it out, make sure that your python.exe does not load modules from that directory and would run the script and add missing modules until it works.
I you have multiple scripts like this, then this manual work is not really the way to go. But in this case also having lots of different .py files in a directory is not a good deployment technique and you should think about packaging them into installable modules and install into your python site-packages.
Still you may use snakefood package to find our the dependencies (has already been discussed here). Again, it just cannot be 100% accurate, but should give you an easy start.
you should be able to extract the needed information from a so called call graph
See for example
http://pycallgraph.slowchop.com/ or
http://blog.prashanthellina.com/2007/11/14/generating-call-graphs-for-understanding-and-refactoring-python-code/
Also, py2exe converts a python call into an executable and in this process it gathers all used modules. I think py2exe is cross platform
I'd try 2½ solutions, one elaborate and 1½ quick-and-dirty:
elaborate: a custom import hook, logging all imports
quick and dirty, part a: os.utime the *.py[co]? (re notation, not glob) files to having access times of yesterday, then run the program and collect all recent access times. Prerequisite: a filesystem that marks access times (by itself and by its mount options).
quick and dirty, part b: remove all *.py[co] files (same in glob and re notation), run the program, see which have been created. Prerequisite: user should have write access to the folder.

Categories

Resources