Python analog for linux "file" command [duplicate] - python

This question already has an answer here:
Is there a python-equivalent of the unix "file" utility?
(1 answer)
Closed 9 years ago.
I'm looking for file linux command analog made in Python. It should provide information about file type as described in man file. Minimal feature set I'm looking for is to determine if file is raw or text (human-readable) one. Wrapper library will be good suggestion.
I know, I can run file as subprocess and grab it's output to determine file type. But my program is supposed to parse thousands of files and I'm afraid of very long execution time in this case.

you need to check the "magic" byte of the file, and I was about to tell you about:
python-magic
when it occured to me that this question should already have been answered on SO, and it has.
N.B.: I'm not listing pymagic as the other post does, as it did not get any update since 0.1 which looks quite old (even the source website is down).
for OSX:
brew install libmagic
pip install python-magic
python
>>> magic.from_file('test.py')
'Python script, ASCII text executable'

Related

Python: Having difficulty obtaining current path of script [duplicate]

This question already has answers here:
How do I get the path of the Python script I am running in? [duplicate]
(5 answers)
Closed 7 years ago.
I'm very new to programming, and to Python. I'm on Mac OSX, trying to work with PyCharm. I've looked at this, this, this, and several more.
But I can't seem to get the path of the current running file. If I use os.getcwd(), I get
'/Users/AlanH/Python'
When really, the full file path is:
'/Users/AlanH/Python/Exercises/PythonBasics/starthere.py'
So I don't understand why that doesn't work.
If I try using sys.path[0], it prings up a path that takes me to my Library, then dives in to Enthought (don't know why, even though I'm using Anaconda distribution).
I could go on and on about all the possible solutions I've tried, but nothing works. All I want is to get the exact path up to current running file. So either
'/Users/AlanH/Python/Exercises/PythonBasics/starthere.py'
or this
'/Users/AlanH/Python/Exercises/PythonBasics'
will do.
Could someone please help?
import os
print(os.path.abspath(__file__))
Locally tested with the following result:
eb#cube ~/Share $ python stackoverflow/path.py
/home/eb/Share/stackoverflow/path.py

Platform independence in pip requirement files [duplicate]

This question already has answers here:
Operating-system specific requirements with pip
(2 answers)
Closed 3 years ago.
How can I get platform specific things into a requirements file? Some windows packages are needed instead of their linux counterparts.
WinPExpect vs pexpect
pywin32 isn't needed on linux but is needed by winpexpect
Any idea of how you could deal with that?
I've thought about a small python script that would detect the platform and deal with it by running pip with different platform specific files as well as the "main" requirements file. Seems like maybe it should be simpler.
pip requirements do not allow it.
There is a discusion about this feature in distutils2. See PEP 345 for more metadata information.
More info about distutils2 and metadata: http://packages.python.org/Distutils2/library/distutils2.metadata.html
I don't know the state of PEP 345 and package installers.
Since this comes up first in a stackoverflow search for [python] requirements different platforms allow me to point to this later question's answer and this one's; especially since the distutils2 links in the other answer here are now dead.

How can I compile a Pygame program to a Windows .exe [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I make an EXE file from a Python program?
I have found several links talking about what to do, but I am unsure of how to use them. They often say to just use the code, but they don't say if I should put it in the terminal or use it to make a program to run. Many recommend the use of py2exe but when I try to use it it will not run because it says I do not have python 2.6 in my registery. I am rather new to the more complicated side of programming and any help would be appreciated. I am running windows vista.
This is certainly a duplicate question, but I'd recommend using py2exe. We probably need more information on how or why you are failing.

Are cx-freezed executables hackable [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can EXE generated by cx_freeze be completely decompiled back to readable Python code?
I've used cx-freeze to convert a python script to an executable. Is it possible to reverse that? Can I get my python script from the binary cx-freeze has generated?
EDIT. This is a practical question. I really need to recover one cx-freezed executable to a more-less readable form. Does anyone have a recipie of doing it?
Any 'frozen' PY application can be disassembled but not really decompiled. With enough time and patience someone would be able to reverse-engineer nearly any program (including yours).
Edit
Correction as per comments below.

How to watch a directory for changes? [duplicate]

This question already has answers here:
Monitoring contents of files/directories? [duplicate]
(5 answers)
Closed 10 years ago.
Could not find anything in python core to do this. Can anyone recommend a library or "battery" to do this? Ideally I would like this to be portable but it's OK if it is available only for Unix (my server).
On Linux, you could be interested in pyinotify
https://github.com/seb-m/pyinotify
Other related libraries:
http://people.gnome.org/~veillard/gamin/python.html
Python FAM interface: http://python-fam.sourceforge.net/
http://gorakhargosh.github.com/watchdog/
I don't think there's something portable for this kind of requirement.
That's too close to the OS IMO.
Otherwise for Linux, there's pynotify.
pyinotify is a binding for Linux inotify kernel filesystem notification subsystem.
Works quite well.
I was just looking for a python package that watches file modifications. Just stumbled upon pywatch and it might just be what you're looking for. It's very simple, but does what I need (fixing pyScss' lack of a watcher).
http://pypi.python.org/pypi/pywatch

Categories