2012-10-06 2 views
0

Automater 워크 플로를 복사하려고하는데 설정 파일에이 워크 플로를 나열했습니다. 구성 파일을 반복하고 디렉토리를 복사하려고합니다. 그들은 이름에 공백이 있으며 문제가 있습니다.디렉토리를 복사하는 데 문제가 있습니다.

그것은 올바르게 등 파일 이름을 인쇄하지만 복사는 복사

import os 
import shutil 

confdir=os.getenv("my_config") 
dropbox=os.getenv("dropbox") 
conffile = ('services.conf') 
conffilename=os.path.join(confdir, conffile)  
sourcedir= (r'~/Library/Services/') 
destdir=os.path.join(dropbox, "My_backups") 

for file_name in open(conffilename): 
    sourcefile=os.path.join(sourcedir, repr(file_name.strip())) 
    print sourcefile 
    destfile=os.path.join(destdir, file_name.strip()) 
    shutil.copytree(sourcefile, destfile) 

로 이름 주위에 ""추가로있을 것 같습니다으로 실패하고 오류 사전

에서

~/Library/Services/'Add PDF Metadata.workflow' 
Traceback (most recent call last): 
    File "Untitled 3.py", line 15, in <module> 
    shutil.copytree(sourcefile, destfile) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py",  line 168, in copytree 
    names = os.listdir(src) 
OSError: [Errno 2] No such file or directory: "~/Library/Services/'Add PDF Metadata.workflow'" 

감사입니다

나는 아래의 제안을 시도했지만 여전히 작동하지 않습니다.

+0

내가 그것을 해결하기 위해 관리하는 모든 여기 http://stackoverflow.com/questions/13052341/i-seem-to-get-an-error-although-the-script-runs-i-cant- 왜 - 왜 – geekcomputers

답변

0

전체 경로를 입력하지 않았습니다. 한 번 더 편집했고 sourcedir = os.path.expanduser ('~/Library/Services /')를 사용하여 홈 디렉토리를 확장했습니다.

1

repr()file_name.strip()에 왜 사용하고 있습니까? 파일 이름을 작은 따옴표로 묶으므로 파일 경로에 표시되지 않습니다. repr()을 제거하면 올바르게 작동합니다.

+0

답장을 보내 주셔서 감사합니다. 이름에 공백이 있기 때문에 파일 이름에 repr()을 사용하고있었습니다. 나는 repr()없이 시도했지만 여전히 동일한 오류가 발생합니다 – geekcomputers

+0

그리고 그 오류 메시지의 파일이 원본 디렉토리에 있는지 확실합니까? –

+0

예 Macintosh-3 : 구성 craigdba $ ls -l ~/Library/Services/ 총계 0 drwxr-xr-x @ 3 craigdba craigdba 102 1 10 월 17:25 PDF 메타 데이터 추가 워크 플로우 drwxr-xr-x @ 3 craigdba craigdba 102 1 10 월 17:17 스포트 라이트 덧글 추가 .workflow drwxr-xr-x 3 craigdba admin 102 20 11 월 2007 NoteBookHelper.service – geekcomputers

0

shutil.copytree(src, dst)src에있는 디렉토리 트리 (및 그 안에있는 모든 파일)를 dst에있는 새 디렉토리 트리에 반복적으로 복사합니다. 파일과 함께 사용할 수 없습니다.

여기서 전체 파일이 아닌 전체 파일을 복사하려면 shutil.copy 또는 shutil.copy2을 사용해야합니다. 파일이 당신이 재생하려는 디렉토리 트리에있을 수 있습니다 경우

, 당신은 실제로 destfile에 파일을 복사 할 shutil.copy(sourcefile)를 호출하기 전에 os.path.dirname(destfile)에 의해 반환되는 경로에 대한 os.makedirs를 사용할 수 있습니다.

그러나 이미 대상이있는 os.makedirs을 호출하면 오류가 발생하므로 try/except을 원할 수 있습니다.

+0

복사 할 디렉토리입니다. – geekcomputers

+0

@geekcomputers 오, 네 말이 맞아, "~/Library/Services/'PDF 메타 데이터 추가. 워크 플로우'"'나를 혼란스럽게 만들었습니다. 'repr()'호출을 제거 했으므로 새로운 오류 메시지를 게시 할 수 있습니까? –

+0

OSError : [Errno 2] 해당 파일이나 디렉토리가 없습니다. '~/Library/Services/Add PDF Metadata.workflow' – geekcomputers

관련 문제