2009-04-05 4 views
1

이 코드가 실패한 이유를 말씀해주십시오. 나는 새로운데 왜 내 우편 번호 형식이 올바르지 않은지 이해할 수 없습니다. 나는 최선의 의사 소통 방법을 모르기 때문에 코드, 오류 메시지 및 내가 생각하는 것을 보여줄 것입니다. zip_command 전에Python 3 : zip 모듈 인수의 형식을 올바르게 지정하십시오 (newb)

Enter a comment --> 
"C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_dir\20090 
405\134614.zip" 
"C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_list" 
Traceback (most recent call last): 
    File "C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_ve 
r5.py", line 32, in <module> 
    zip_command = zipfile.ZipFile(target, 'w').write(source) 
    File "c:\python30\lib\zipfile.py", line 683, in __init__ 
    self.fp = io.open(file, modeDict[mode]) 
    File "C:\Python30\lib\io.py", line 222, in open 
    closefd) 
    File "C:\Python30\lib\io.py", line 615, in __init__ 
    _fileio._FileIO.__init__(self, name, mode, closefd) 
IOError: [Errno 22] Invalid argument: '"C:\\Documents and Settings\\Benjamin Ser 
rato\\My Documents\\python\\backup_dir\\20090405\\134614.zip"' 

두 인쇄 테스트는 두 문자열이 제대로 zipfile.ZipFile()에 전달되고 있다는 것을 말해 할당됩니다

#!c:\python30 
# Filename: backup_ver5.py 

import os 
import time 
import zipfile 


source = r'"C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_list"' 

target_dir = r'C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_dir' 

today = target_dir + os.sep + time.strftime('%Y%m%d') 

now = time.strftime('%H%M%S') 

comment = input('Enter a comment --> ') 

if len(comment) == 0: 
    target = '"' + today + os.sep + now + '.zip' + '"' 
else: 
    target = '"' + today + os.sep + now + '_' + \ 
    comment.replace(' ', '_') + '.zip' + '"' 

if not os.path.exists(today): 
    os.mkdir(today) 
    print('Successfully created directory', today) 


print(target) 
print(source) 
zip_command = zipfile.ZipFile(target, 'w').write(source) 

if os.system(zip_command) == 0: 
    print('Successful backup to', target) 
else: 
    print('Backup FAILED') 

    enter code here 

는이 오류 메시지를받을. 그 추적은 내가 zipfile.ZipFile()이라고 정확하게 부르지 않는다고 알려줍니다. __init__의 오류로 인해이 점에 대해 더 확신 할 수 있습니다. 마지막으로 문제는 내 경로 문자열에 이중 백 슬래시가 발생하게하는 것 같습니다. 나는 왜 IOError가 그것을 보여줄 수 있는지를 알 수 없다.

this site을 사용하여 zipfile의 사용법을 알아 냈습니다. zipfile은 클래스이며, 프로그램의 시작 부분에서 가져 와서 기본 방법으로 사용합니다. 쓰기를 원하는 파일을 zipfile.ZipFile('file to write', 'mode')으로 전달하고 쓰기 가능하도록 설정된 객체를 열도록 프로그램을 설정합니다. 그런 다음 명령은 대상 폴더에 "".zipfile('files to write')과 같은 하위 메서드를 사용하여 파일을 씁니다.

어디로 잘못 가고 있습니까?

+0

내가 삭제 한 따옴표와 아직 채워지지 않은 따옴표가 명확하지 않습니다. 게시물의 현재 상태를 보여줄 수 있습니까? 지금보고있는 오류는 무엇입니까? – DNS

답변

3

경로 이름 주위에 여분의 쌍 따옴표가 있기 때문에 표시되는 것처럼 보입니다. 큰 따옴표를 제거하고 제대로 작동하는지 확인하십시오.

+0

맞습니다. 외부 프로그램을 사용할 때 명령 줄에 인용 된 경로를 전달해야한다고 생각했습니다. 모든 것이 이미 문자열이므로 따옴표를 제거했습니다. if/else에서 대상 문자열에 대해 따옴표를 제거했습니다. 나는 아직도 대략 동일한 오류를받습니다. –

+0

원본 문자열에서도 제거를 시도 했습니까? –

1

다른 질문에 대답하십시오 : escaped이기 때문에 이중 백 슬래시가 있습니다.

관련 문제