2016-05-31 4 views
0

파이썬에서 데스크톱 응용 프로그램을 만들었으며 응용 프로그램 창에 표시된 파일을 복사 할 수있는 권한을 사용자에게 부여해야합니다. 은 Windows 탐색기가 아닌이므로 붙여 넣기 할 수 있습니다. 그들이 원하는 곳에서.Windows에서 파이썬으로 파일을 복사하는 방법

처럼 " 또는 "ctrl + V "을 마우스 오른쪽 버튼으로 클릭하고 복사하십시오. 난 단지 파일이

나타납니다 "붙여 넣기"나는 사용자가 클릭 그래서 메모리에 저장하려면 그러나

shutil.copy(src, dst) 
Copy the file src to the file or directory dst. If dst is a directory, a file with the same basename as src is created (or overwritten) in the directory specified. Permission bits are copied. src and dst are path names given as strings. 

또 shutil 사용하는 하나의 디렉토리에서 파일을 복사하는 파이썬 기능을 발견했습니다

어떻게 이것을 할 수 있습니까?

+1

"python add to clipboard"검색을 시도한 적이 있습니까? 코드는 실제로 한 소스 대상에서 다른 대상으로 실제로 복사하는 것과 관련이 있습니다. 귀하의 질문은 귀하가 성취하고자하는 것을 연구, 코드화 및 테스트하도록 요구하고 있기 때문에 질문보다는 과제가 더 많습니다. 그것은이 공동체가 서로에게 일반적으로하는 일이 아닙니다. FAQ를 보거나 문제를 좀 더 관련성이 높고 달성하고자하는 것과 더 가깝게 해결하려는 일종의 노력을 보여주는 코드로 질문을 업데이트하십시오. – Torxed

+1

예, 검색했지만 일반 텍스트 또는 이미지 유형 파일을 클립 보드에 복사하는 방법을 찾았습니다. 글쎄, 어떻게 해야할지 모르는 코드를 작성할 수 없다. –

답변

3

필요한 클립 보드 형식은 CF_HDROP입니다. 예 :

import ctypes 
from ctypes import wintypes 
import pythoncom 
import win32clipboard 

class DROPFILES(ctypes.Structure): 
    _fields_ = (('pFiles', wintypes.DWORD), 
       ('pt',  wintypes.POINT), 
       ('fNC', wintypes.BOOL), 
       ('fWide', wintypes.BOOL)) 

def clip_files(file_list): 
    offset = ctypes.sizeof(DROPFILES) 
    length = sum(len(p) + 1 for p in file_list) + 1 
    size = offset + length * ctypes.sizeof(ctypes.c_wchar) 
    buf = (ctypes.c_char * size)() 
    df = DROPFILES.from_buffer(buf) 
    df.pFiles, df.fWide = offset, True 
    for path in file_list: 
     array_t = ctypes.c_wchar * (len(path) + 1) 
     path_buf = array_t.from_buffer(buf, offset) 
     path_buf.value = path 
     offset += ctypes.sizeof(path_buf) 
    stg = pythoncom.STGMEDIUM()  
    stg.set(pythoncom.TYMED_HGLOBAL, buf) 
    win32clipboard.OpenClipboard() 
    try: 
     win32clipboard.SetClipboardData(win32clipboard.CF_HDROP, 
             stg.data) 
    finally: 
     win32clipboard.CloseClipboard() 

if __name__ == '__main__': 
    import os 
    clip_files([os.path.abspath(__file__)]) 
관련 문제