If I use xattr on Python on macOS to display a file's comments, it shows extraneous data:
>>> from xattr import xattr
>>> from pprint import pprint
>>> pprint(xattr('tmp.pk.new')[u'com.apple.metadata:kMDItemFinderComment'])
'bplist00_\x10\x0fExample comment\x08\x00\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1a'
>>>
The comment is just 'Example comment', so what is the other data that's being displayed?
The finder comment is saved in a binary property list format, so you have to decode it to access the contents. In Python 3 you can just use the standard library module plistlib:
>>> from plistlib import loads
>>> from xattr import xattr
>>> contents = xattr('tmp.pk.new')['com.apple.metadata:kMDItemFinderComment']
>>> loads(contents)
'Example comment'
If you are still in Python 2, as your code suggests, you have to use an external library, as the buildin plistlib does not support the binary format, for example:
pip install biplist
python
>>> from biplist import readPlistFromString
>>> from xattr import xattr
>>> contents = xattr('tmp.pk.new')['com.apple.metadata:kMDItemFinderComment']
>>> readPlistFromString(contents)
'Example comment'
Related
I have a build.properties file like
[directory]
src = src
srcdir = ${src}/fix
srcext = ${srcdir}/extensions
srct = ${srcext}/xyz
my aim is to get the full value of srct i mean it is to be src/fix/extensions/xyz
Is there any way of getting the value of cde? I tried configparser and jproperties but i couldn't get the required output.
You can use ExtendedInterpolation class which implements more advanced syntax.
>>> from configparser import ConfigParser, ExtendedInterpolation
>>>
>>> parser = ConfigParser(interpolation=ExtendedInterpolation())
>>> parser.read("build.properties")
['build.properties']
>>> print(parser["directory"]["srct"])
src/fix/extensions/xyz
I am parsing a file with ini format using configparser module in python 2. I am using configparser instead of ConfigParser so that code is compataible with python 3.
Problem that with python 2 parsed "value " is coming as unicode string as u'value'.
Please note that with python 3 parsed value is same as python2 if I would use ConfigParser instead.
Is there a way to resolve this?
Sample config file:
$ cat test_config
[test]
field = value
Python run
$ python
Python 2.7.14 (default, Nov 2 2017, 17:39:03)
>>> import configparser
>>> config_name = "test_config"
>>>
>>> config_parser = configparser.SafeConfigParser()
>>> config_parser.optionxform = str
>>> config_parser.read(config_name)
>>> config = {}
>>> mode = 'test'
>>> for item_tuple in config_parser.items(mode):
... config[item_tuple[0]] = item_tuple[1]
...
>>> print(config['field'])
value
>>> config['field']
u'value'
>>>
I load a .pyd file as python module.
Under windows I see a version when I do a right click->Properties->"Details"-Tab
How can I read (in python) the fileversion of this pyd-file?
Exists a function or something in python to read this version?
Using win32api.GetFileVersionInfo:
>>> import win32api
>>> path = r'c:\python27\lib\site-packages\win32\win32api.pyd'
>>> info = win32api.GetFileVersionInfo(path, '\\')
>>> '{}.{}.{}.{}'.format(
... win32api.HIWORD(info['FileVersionMS']),
... win32api.LOWORD(info['FileVersionMS']),
... win32api.HIWORD(info['FileVersionLS']),
... win32api.LOWORD(info['FileVersionLS']))
'2.7.218.0'
I got module name which contains declaration of os.path.isfile. Jedi lib gave me genericpath (without file path). Now i want to get full filename of PY file with this genericpath module. E.g. "C:\Py27\Lib\genericpath.py". How can I do it? Jedi cannot do it?
You could check the value of __file__:
>>> import genericpath
>>> genericpath.__file__
'/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/genericpath.pyc'
When __file__ doesn't work, do it the proper way—use inspect:
>>> import somemodule
>>> import inspect
>>> inspect.getfile(somemodule)
'/usr/lib64/python2.7/somemodule.pyc'
Like this:
>>> import re
>>> re.__file__
'/usr/lib/python2.7/re.pyc'
For packages that are not part of the Python core, you can also use __path__:
>>> import requests
>>> requests.__file__
'/usr/local/lib/python2.7/dist-packages/requests-1.1.0-py2.7.egg/requests/__init__.pyc'
>>> requests.__path__
['/usr/local/lib/python2.7/dist-packages/requests-1.1.0-py2.7.egg/requests']
Get module filename without loading it:
print(importlib.util.find_spec("urllib.request", None).origin)
I've seen several approaches for finding the path of a module by first importing it. Is there a way to do this without importing the module?
Using pkgutil module:
>>> import pkgutil
>>> package = pkgutil.get_loader("pip")
>>> package.filename
'/usr/local/lib/python2.6/dist-packages/pip-0.7.1-py2.6.egg/pip'
>>> package = pkgutil.get_loader("threading")
>>> package.filename
'/usr/lib/python2.6/threading.py'
>>> package = pkgutil.get_loader("sqlalchemy.orm")
>>> package.filename
'/usr/lib/pymodules/python2.6/sqlalchemy/orm'
In Python 3, use pkgutil.get_loader("module name").get_filename() instead.
Using imp module:
>>> import imp
>>> imp.find_module('sqlalchemy')
(None, '/usr/lib/pymodules/python2.6/sqlalchemy', ('', '', 5))
>>> imp.find_module('pip')
(None, '/usr/local/lib/python2.6/dist-packages/pip-0.7.1-py2.6.egg/pip', ('', '', 5))
>>> imp.find_module('threading')
(<open file '/usr/lib/python2.6/threading.py', mode 'U' at 0x7fb708573db0>, '/usr/lib/python2.6/threading.py', ('.py', 'U', 1))
N.B: with imp module you can't do something like imp.find_module('sqlalchmy.orm')
For python3 imp is deprecated. Use pkgutil (as seen above) or for Python 3.4+ use importlib.util.find_spec:
>>> import importlib
>>> spec = importlib.util.find_spec("threading")
>>> spec.origin
'/usr/lib64/python3.6/threading.py'
You might want to try running this in your interpreter:
>>> import sys
>>> sys.modules['codecs'].__file__ # codecs is just an example
'/usr/lib/python2.7/codecs.pyc'