Bitte sagen Sie mir, warum dieser Code fehlschlägt. Ich bin neu und ich verstehe nicht, warum meine Formatierung meiner Zip-Argumente falsch ist. Da ich nicht sicher bin, wie ich am besten kommunizieren soll, zeige ich den Code, die Fehlermeldung und was ich glaube, passiert.Python 3: Zip-Modul Argumente richtig formatieren (newb)
#!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
Ich erhalte diese Fehlermeldung:
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"'
Die beiden Drucktests vor zip_command
zugewiesen wird mir sagen, dass die beiden Strings zu zipfile.ZipFile()
korrekt übergeben werden. Das Traceback sagt mir, dass ich zipfile.ZipFile()
nicht korrekt anrufe. Der Fehler in __init__
macht mich sicherer. Zuletzt scheint das Problem zu sein, dass ich meine Pfadzeichenfolge doppelte Backslashes habe. Ich kann nicht nachvollziehen, warum der IOError das zeigt.
Ich habe this site verwendet, um herauszufinden, wie man zipfile
verwendet. zipfile
ist eine Klasse, ich importiere sie am Anfang des Programms, dann benutze ich sie und ihre primäre Methode. Ich gebe die Datei, die ich schreiben möchte, an zipfile.ZipFile('file to write', 'mode')
weiter und setze das Programm so, dass ein Objekt geöffnet wird, das beschreibbar ist. Dann schreibt der Befehl die Datei in den Zielordner mit einer Untermethode wie "".zipfile('files to write')
.
Wohin gehe ich falsch?
Ich bin nicht klar, welche Zitate Sie entfernt haben und welche nicht; Können Sie Ihren Post bearbeiten, um den aktuellen Status des Codes anzuzeigen, und welchen Fehler sehen Sie jetzt? – DNS