Right.  You can't call a Windows service unless you are on Windows.

You will want to build a "shim" function which you call with a generic name.

It looks at what operating system it is running on, and then calls the
appropriate operating system specific code to perform the operation.  The
import of the operating system specific routines is done by that inner
routine. (I will use dots to replace spaces below.)

import sys
def launch_file_explorer(foo, bar):
..if sys.platform == 'linux2':
....launch_linux_explorer(foo, bar)
..else:
....launch_win_explorer(foo, bar)

def launch_win_explorer(foo, bar):
..from win32.com import shell, shelcon
..# etcetera

Remember that "import" is an executable statement.  It is not like the
"include" compiler directive you may be used to from a "C" like language.
It does not look for the imported module until you tell it to at run time.
If you never execute the "import" statement, no one cares whether the
module is actually present or whether it works.  Have you ever tried typing
"import antigravity" at a Python command prompt?  I think that is the sort
of action you want.

Look at the standard library source (it is in your Python distribution
folders) and read 'antigravity.py'  which in turn calls 'webbrowser.py' and
see how the experts do it.  Happy coding.
--
Vernon Cole



On Fri, Dec 13, 2013 at 1:02 PM, iMath <2281570...@qq.com> wrote:

> I want to build a cross-platform application ,I used a windows API called
> SHOpenFolderAndSelectItems()<http://msdn.microsoft.com/en-us/library/windows/desktop/bb762232%28v=vs.85%29.aspx>.
> Although I found example called it by pywin32,but pywin32 is not available
> on Linux , I don't want to call a Windows API on linux,just don't want to
> make another code version for Linux,so I wonder how to access it by ctypes?
> yes,this API cannot be called on Linux ,I just want to make it silent in
> the code so that I can freeze the Python scripts into executables by
> cx_Freeze without pywin32 module-missing error happend .
>
> from win32com.shell import shell, shellconimport osdef 
> launch_file_explorer(path, files):
>      '''
>      Given a absolute base path and names of its children (no path), open
>      up one File Explorer window with all the child files selected
>      '''
>      folder_pidl = shell.SHILCreateFromPath(path,0)[0]
>      desktop = shell.SHGetDesktopFolder()
>      shell_folder = desktop.BindToObject(folder_pidl, 
> None,shell.IID_IShellFolder)
>      name_to_item_mapping = dict([(desktop.GetDisplayNameOf(item, 
> shellcon.SHGDN_FORPARSING|shellcon.SHGDN_INFOLDER), item) for item in 
> shell_folder])
>      print(name_to_item_mapping)
>      to_show = []
>      for file in files:
>          if file in name_to_item_mapping:
>              to_show.append(name_to_item_mapping[file])
>          # else:
>              # raise Exception('File: "%s" not found in "%s"' % (file, path))
>
>
>      shell.SHOpenFolderAndSelectItems(folder_pidl, to_show, 0)
>
>
>
>
>
>
> p=r'E:\aa'print(os.listdir(p))
> launch_file_explorer(p, os.listdir(p))
>
>
>
> _______________________________________________
> python-win32 mailing list
> python-win32@python.org
> https://mail.python.org/mailman/listinfo/python-win32
>
>
_______________________________________________
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32

Reply via email to