이 코드가 실패한 이유를 말씀해주십시오. 나는 새로운데 왜 내 우편 번호 형식이 올바르지 않은지 이해할 수 없습니다. 나는 최선의 의사 소통 방법을 모르기 때문에 코드, 오류 메시지 및 내가 생각하는 것을 보여줄 것입니다. 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')
과 같은 하위 메서드를 사용하여 파일을 씁니다.
어디로 잘못 가고 있습니까?
내가 삭제 한 따옴표와 아직 채워지지 않은 따옴표가 명확하지 않습니다. 게시물의 현재 상태를 보여줄 수 있습니까? 지금보고있는 오류는 무엇입니까? – DNS