python练习贴08 读写剪切板

同事经常会给我发一些这样的路径,

\\Server\DepA\ProjB\other\...\...\aDir\

\\Server\DepA\ProjB\other\...\...\aDir\aFile

 

然后我需要代开他们,看一看,或者追加一些内容。

这一过程很繁琐,于是今天的需求便是写一个python脚本。

直接打开剪切板中的资源,如果他是一个合法路径的话。

 

其中下面这两个方法是读取和设置剪切板

getClipboardText
setClipboardText

使用的是pywin32

 

 

__author__="wjason"
__date__ ="$2009-10-28 14:56:42$"

import os.path
import win32clipboard
import win32con

def openResource(path):
    if not os.path.exists(path):
        return

    if os.path.isdir(path):
        cmd = 'cmd /C call explorer "'+path+'"'
    else:
        cmd = 'cmd /C call "'+path+'"'
    
    #os.popen(cmd)
    #os.system(cmd)
    import subprocess
    subprocess.Popen(cmd, shell=True)

def getClipboardText():
    win32clipboard.OpenClipboard()
    result = win32clipboard.GetClipboardData(win32con.CF_TEXT)
    win32clipboard.CloseClipboard()
    return result

def setClipboardText(aString):
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardData(win32con.CF_TEXT, aString)
    win32clipboard.CloseClipboard()


if __name__ == "__main__":
    print getClipboardText()
    openResource(getClipboardText())


 

 

 

 

 

 

 

 

你可能感兴趣的:(C++,c,python,OS,C#)