Ich versuche ein Programm zu erstellen, das eine Sicherung speichert, indem ein Verzeichnis für eine Zip-Datei erstellt: Dies ist eine Übung aus Ein Byte von Python (ich werde das volle Beispiel geben, damit ihr sehen könnt, wohin er geht.) Der Beispielcode ist:makedirs() gibt AttributeError: 'int' Objekt hat kein Attribut 'rfind'
#! /usr/bin/env python3
import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = ['~/Desktop/python']
# 2. The backup must be stored in a main backup directory
target_dir = '~/Dropbox/Backup/' # Remember to change this to what you'll be using
# 3. The files are backed up into a zip file.
# 4. the name of the zip archive is the current date and time
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') +'.zip'
now = time.strftime('%H%M%S')
# Create the subdirectory if it isn't already there.
if not os.path.exists(today):
os.mkdir(today) # make directory
print('Successfully created directory', today)
# The name of the zip file
target = today + os.sep + now + '.zip'
# 5. We use the zip command to put the files in a zip archive
zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
print(zip_command)
# Run the backup
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')
Dies zieht den Fehler:
Traceback (most recent call last):
File "backup_ver2.py", line 23, in <module>
os.mkdir(today) # make directory
TypeError: mkdir: illegal type for path parameter
Meine Lösung :
import os
import time
today = 14052016 # I set today as a string to solve a previous issue.
.....
# Create the subdirectory if it isn't already there.
if not os.path.exists(today):
os.makedirs(today, exist_ok=True) # make directory
print('Successfully created directory', today)
, die den Fehler verursacht: Ich weiß, ich bin in Schwierigkeiten so jetzt
Traceback (most recent call last):
File "backup_ver2a.py", line 23, in <module>
os.makedirs(today, exist_ok=True) # make directory
File "/usr/lib/python3.4/os.py", line 222, in makedirs
head, tail = path.split(name)
File "/usr/lib/python3.4/posixpath.py", line 103, in split
i = p.rfind(sep) + 1
AttributeError: 'int' object has no attribute 'rfind'
Diese Zurückverfolgungs referenziert Zeilen im Modul. Ist es möglich, dass die Variable "heute" immer noch im Mittelpunkt dieser beiden Fehler steht? Gibt es eine bessere Möglichkeit, heute zu definieren, um nicht so viele Fehler zu ziehen, oder gibt es eine bessere Möglichkeit, ein Unterverzeichnis zu prüfen und zu erstellen? Wenn Sie in seinem Beispiel mehr Fehler bemerken, korrigieren Sie sie bitte nicht. Ich bin sicher, ich werde sie bald finden. :) Danke für jede Hilfe.
Hinweise: Ich bin mit Ubuntu 14.04 LTS und Verwendung Python 3
'today = 14052016' das widerspricht mit dem, was Sie" eine Zeichenfolge "gesagt haben. String muss zitiert werden – gdlmx