Python ntpath.py上的特殊错误

Python ntpath.py上的特殊错误,python,windows-7,path,subprocess,Python,Windows 7,Path,Subprocess,我正在尝试在Windows7上的VLCPlayer上编写一个类似于包装器的东西,这样它就可以通过击键加载文件夹中的下一个和上一个文件。我在其中所做的是,我将一个filepath作为参数,创建一个vlc类的实例,并使用pyHook扫描键,当检测到特定的击键时,调用实例上的play_next和play_prev方法。这些方法的工作原理是杀死最后一个进程,并用get_new_file方法找到的下一个文件生成新的vlc。它先工作几次,然后给出特殊的错误 None None Traceback

我正在尝试在Windows7上的VLCPlayer上编写一个类似于包装器的东西,这样它就可以通过击键加载文件夹中的下一个和上一个文件。我在其中所做的是,我将一个filepath作为参数,创建一个vlc类的实例,并使用pyHook扫描键,当检测到特定的击键时,调用实例上的play_next和play_prev方法。这些方法的工作原理是杀死最后一个进程,并用get_new_file方法找到的下一个文件生成新的vlc。它先工作几次,然后给出特殊的错误

None   
None   
Traceback (most recent call last):  
  File "C:\Python27\pyHook\HookManager.py", line 351, in KeyboardSwitch   
    return func(event)   
  File "filestuff.py", line 64, in kbwrap   
    kbeventhandler(event,instance)   
  File "filestuff.py", line 11, in kbeventhandler   
    instance.play_prev()   
  File "filestuff.py", line 34, in play_prev   
    f=self.get_new_file(-1)   
  File "filestuff.py", line 40, in get_new_file   
    dirname= os.path.dirname(self.fn)   
  File "C:\Python27\lib\ntpath.py", line 205, in dirname   
    return split(p)[0]   
  File "C:\Python27\lib\ntpath.py", line 178, in split   
    while head2 and head2[-1] in '/\\':   
TypeError: an integer is required
代码如下:

import os
import sys
import pythoncom, pyHook 
import win32api
import subprocess
import ctypes

def kbeventhandler(event,instance):

    if event.Key=='Home':
        instance.play_prev()
    if event.Key=='End':
        instance.play_next()
    return True

class vlc(object):
    def __init__(self,filepath,vlcp):
        self.fn=filepath
        self.vlcpath=vlcp
        self.process = subprocess.Popen([self.vlcpath, self.fn])
    def kill(self):
        PROCESS_TERMINATE = 1
        handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, self.process.pid)
        ctypes.windll.kernel32.TerminateProcess(handle, -1)
        ctypes.windll.kernel32.CloseHandle(handle)
        print self.process.poll()
    def play_next(self):
        self.kill()
        f=self.get_new_file(1)
        self.process = subprocess.Popen([self.vlcpath, f])
        self.fn=f
    def play_prev(self):
        self.kill()
        f=self.get_new_file(-1)
        self.process = subprocess.Popen([self.vlcpath, f])
        self.fn=f

    def get_new_file(self,switch):

        dirname= os.path.dirname(self.fn)    
        supplist=['.mkv','.flv','.avi','.mpg','.wmv']
        files = [os.path.join(dirname,f) for f in os.listdir(dirname) if (os.path.isfile(os.path.join(dirname,f)) and os.path.splitext(f)[-1]in supplist)]
        files.sort()
        try: currentindex=files.index(self.fn)
        except: currentindex=0
        i=0
        if switch==1:
            if currentindex<(len(files)-1):i=currentindex+1

        else:
            if currentindex>0:i=currentindex-1

        return files[i]    

def main():
    vlcpath='vlc'
    if os.name=='nt': vlcpath='C:/Program Files (x86)/VideoLAN/VLC/vlc.exe'
    fn='H:\\Anime\\needless\\Needless_[E-D]\\[Exiled-Destiny]_Needless_Ep11v2_(04B16479).mkv'
    if len(sys.argv)>1:
        fn=sys.argv[1] #use argument if available or else use default file
    instance=vlc(fn,vlcpath)
    hm = pyHook.HookManager()
    def kbwrap(event):
        kbeventhandler(event,instance)
    hm.KeyDown = kbwrap
    hm.HookKeyboard()    
    pythoncom.PumpMessages()

if __name__ == '__main__':
    main() 
导入操作系统
导入系统
导入pythoncom、pyHook
导入win32api
导入子流程
导入ctypes
def kbeventhandler(事件、实例):
如果event.Key==“Home”:
实例.play_prev()
如果event.Key=='End':
实例。播放下一个()
返回真值
类vlc(对象):
定义初始化(self、文件路径、vlcp):
self.fn=filepath
self.vlcpath=vlcp
self.process=subprocess.Popen([self.vlcpath,self.fn])
def杀死(自我):
进程\u终止=1
handle=ctypes.windell.kernel32.OpenProcess(PROCESS\u TERMINATE,False,self.PROCESS.pid)
ctypes.windl.kernel32.TerminateProcess(句柄,-1)
ctypes.windl.kernel32.CloseHandle(句柄)
打印self.process.poll()
def播放下一个(自我):
self.kill()
f=自我。获取新文件(1)
self.process=subprocess.Popen([self.vlcpath,f])
self.fn=f
def play_prev(自我):
self.kill()
f=self.get_new_文件(-1)
self.process=subprocess.Popen([self.vlcpath,f])
self.fn=f
def get_new_文件(自身,开关):
dirname=os.path.dirname(self.fn)
supplist=['.mkv'、'.flv'、'.avi'、'.mpg'、'.wmv']
files=[os.path.join(dirname,f)表示os.listdir(dirname)中的f,如果(os.path.isfile(os.path.join(dirname,f))和支持列表中的os.path.splitext(f)[-1]
files.sort()文件
try:currentindex=files.index(self.fn)
除外:currentindex=0
i=0
如果开关==1:
如果currentindex0:i=currentindex-1
返回文件[i]
def main():
vlcpath='vlc'
如果os.name='nt':vlcpath='C:/程序文件(x86)/VideoLAN/VLC/VLC.exe'
fn='H:\\Anime\\underse\\underse\uuu[E-D]\\[流亡的命运]\u underse\u Ep11v2(04B16479).mkv'
如果len(sys.argv)>1:
fn=sys.argv[1]#如果可用,请使用参数,否则使用默认文件
实例=vlc(fn,vlcpath)
hm=pyHook.HookManager()
def kbwrap(事件):
kbeventhandler(事件,实例)
hm.KeyDown=kbwrap
hm.键盘()
pythoncom.PumpMessages()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
这里也是:

问题是 在
main
中,我设置
hm.KeyDown=kbwrap
,然后从函数
kbwrap
调用实际事件处理程序
kbeventhandler
,但没有从
kbwrap
返回任何值

def kbwrap(event):
    return kbeventhandler(event,flag)
hm.KeyDown = kbwrap
我还将vlc工作卸载到另一个线程,因为pyHook对子进程的处理不好。 最终工作代码:

import os
import sys
import pythoncom, pyHook 
import win32api
import subprocess
import ctypes
import threading
from multiprocessing import *

class vlcThread(threading.Thread):
    def __init__(self,filepath,vlcp,fl):
        threading.Thread.__init__(self)
        self.fn,self.vlcpath,self.flag=filepath,vlcp,fl
        self.daemon=True

        self.start() # invoke the run method

    def run(self):
        vlcinstance=vlc(self.fn,self.vlcpath)
        while True:
            if(self.flag.value==1):
                vlcinstance.play_next()
                self.flag.value=0
            if(self.flag.value==-1):
                vlcinstance.play_prev()
                self.flag.value=0







def kbeventhandler(event,flag):

    if event.Key=='Home':
        flag.value =-1
        return False
    if event.Key=='End':
        flag.value =1
        return False
    return True

class vlc(object):
    def __init__(self,filepath,vlcp):
        self.fn=filepath
        self.vlcpath=vlcp
        self.process = subprocess.Popen([self.vlcpath,self.fn],close_fds=True)
    def kill(self):
        p, self.process = self.process, None
        if p is not None and p.poll() is None:
            p.kill() 
            p.wait()


    def play_next(self):
        self.kill()
        f=self.get_new_file(1)
        self.process = subprocess.Popen([self.vlcpath,f],close_fds=True)
        self.fn=f
    def play_prev(self):
        self.kill()
        f=self.get_new_file(-1)
        self.process = subprocess.Popen([self.vlcpath, f],close_fds=True)
        self.fn=f

    def get_new_file(self,switch):

        dirname= os.path.dirname(self.fn)    
        supplist=['.mkv','.flv','.avi','.mpg','.wmv','ogm','mp4']
        files = [os.path.join(dirname,f) for f in os.listdir(dirname) if (os.path.isfile(os.path.join(dirname,f)) and os.path.splitext(f)[-1]in supplist)]
        files.sort()
        try: currentindex=files.index(self.fn)
        except: currentindex=0
        i=0
        if switch==1:
            if currentindex<(len(files)-1):i=currentindex+1

        else:
            if currentindex>0:i=currentindex-1

        return files[i]    



def main():
    vlcpath='vlc'
    flag=Value('i')
    flag.value=0
    if os.name=='nt': vlcpath='C:/Program Files (x86)/VideoLAN/VLC/vlc.exe'
    fn='H:\\Anime\\needless\\Needless_[E-D]\\[Exiled-Destiny]_Needless_Ep11v2_(04B16479).mkv'
    if len(sys.argv)>1:
        fn=sys.argv[1] #use argument if available or else use default file

    t=vlcThread(fn,vlcpath,flag)
    hm = pyHook.HookManager()
    def kbwrap(event):
        return kbeventhandler(event,flag)
    hm.KeyDown = kbwrap
    hm.HookKeyboard()    
    pythoncom.PumpMessages()


if __name__ == '__main__':
    main() 
导入操作系统
导入系统
导入pythoncom、pyHook
导入win32api
导入子流程
导入ctypes
导入线程
从多处理导入*
类vlcThread(threading.Thread):
定义初始化(self、文件路径、vlcp、fl):
threading.Thread.\uuuuu init\uuuuuu(自)
self.fn,self.vlcpath,self.flag=filepath,vlcp,fl
self.daemon=True
self.start()#调用run方法
def运行(自):
vlcinstance=vlc(self.fn,self.vlcpath)
尽管如此:
如果(self.flag.value==1):
vlcinstance.play_next()
self.flag.value=0
如果(self.flag.value==-1):
vlcinstance.play_prev()
self.flag.value=0
def kbeventhandler(事件、标志):
如果event.Key==“Home”:
flag.value=-1
返回错误
如果event.Key=='End':
flag.value=1
返回错误
返回真值
类vlc(对象):
定义初始化(self、文件路径、vlcp):
self.fn=filepath
self.vlcpath=vlcp
self.process=subprocess.Popen([self.vlcpath,self.fn],close_fds=True)
def杀死(自我):
p、 self.process=self.process,无
如果p不是None且p.poll()是None:
p、 杀死
p、 等等
def播放下一个(自我):
self.kill()
f=自我。获取新文件(1)
self.process=subprocess.Popen([self.vlcpath,f],close_fds=True)
self.fn=f
def play_prev(自我):
self.kill()
f=self.get_new_文件(-1)
self.process=subprocess.Popen([self.vlcpath,f],close_fds=True)
self.fn=f
def get_new_文件(自身,开关):
dirname=os.path.dirname(self.fn)
supplist=['.mkv'、'.flv'、'.avi'、'.mpg'、'.wmv'、'ogm'、'mp4']
files=[os.path.join(dirname,f)表示os.listdir(dirname)中的f,如果(os.path.isfile(os.path.join(dirname,f))和支持列表中的os.path.splitext(f)[-1]
files.sort()文件
try:currentindex=files.index(self.fn)
除外:currentindex=0
i=0
如果开关==1:
如果currentindex0:i=currentindex-1
返回文件[i]
def main():
vlcpath='vlc'
标志=值('i')
flag.value=0
如果os.name='nt':vlcpath='C:/程序文件(x86)/VideoLAN/VLC/VLC.exe'
fn='H:\\Anime\\underse\\underse\uuu[E-D]\\[流亡的命运]\u underse\u Ep11v2(04B16479).mkv'
如果len(sys.argv)>1:
fn=sys.argv[1]#如果可用,请使用参数,否则使用默认文件
t=vlcThread(fn,vlcpath,标志)
hm=pyHook.HookManager()
def kbwrap(事件):
返回kbeventhandler(事件,标志)
hm.KeyDown=kbwrap
hm.键盘()
pythoncom.PumpMessages()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
问题是
在
main
中,我设置
hm.KeyDown=kbwrap
,然后从函数
kbwrap
调用实际事件处理程序
kbeventhandler
,但没有从
kbwrap
返回任何值

def kbwrap(event):
    return kbeventhandler(event,flag)
hm.KeyDown = kbwrap
我还卸载了vlc