Ja das stimmt! War auch nur ein schnell Test.
Wie dem auch sei hab ich nun eine weitaus bessere Lösung gefunden, da ich die Fehlermeldung "Zugriff verweigert" nicht umgehen kann.
Ich hab den Befehl shutil.copytree(src, dst) ausprobiert und modifiziert. Anstatt die Dateien zu kopieren, erstellt er nun eine leere Datei mit dem gleichen Namen im Zielverzeichnis(dst).
Zudem wurde die Funktion eingebaut, dass Dateien mit bestimmten Dateiendungen trotzdem kopiert werden können.
Praktisch kann man dies als kleines Addon ins shutil Modul einbauen, habs jetzt aber noch als extra Datei im benötigten Ordner um es in Zukunft nicht zu vergessen.
Code: Alles auswählen
"""Modified version of shutil.copytree for copytree with empty files"""
#Markierung 1 -> emptycopy muss erstellt werden
#Markierung 2 -> statt copy2(srcname, dstname) -> emptycopy(srcname, dstname)
import os
import stat
import errno
class Error(EnvironmentError):
pass
class SpecialFileError(EnvironmentError):
"""Raised when trying to do a kind of operation (e.g. copying) which is
not supported on a special file (e.g. a named pipe)"""
class ExecError(EnvironmentError):
"""Raised when a command could not be executed"""
try:
WindowsError
except NameError:
WindowsError = None
def copyfileobj(fsrc, fdst, length=16*1024):
"""copy data from file-like object fsrc to file-like object fdst"""
while 1:
buf = fsrc.read(length)
if not buf:
break
fdst.write(buf)
def _samefile(src, dst):
# Macintosh, Unix.
if hasattr(os.path, 'samefile'):
try:
return os.path.samefile(src, dst)
except OSError:
return False
# All other platforms: check for same pathname.
return (os.path.normcase(os.path.abspath(src)) ==
os.path.normcase(os.path.abspath(dst)))
def copyfile(src, dst):
"""Copy data from src to dst"""
if _samefile(src, dst):
raise Error("`%s` and `%s` are the same file" % (src, dst))
for fn in [src, dst]:
try:
st = os.stat(fn)
except OSError:
# File most likely does not exist
pass
else:
# XXX What about other special files? (sockets, devices...)
if stat.S_ISFIFO(st.st_mode):
raise SpecialFileError("`%s` is a named pipe" % fn)
with open(src, 'rb') as fsrc:
with open(dst, 'wb') as fdst:
copyfileobj(fsrc, fdst)
def copystat(src, dst):
"""Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
st = os.stat(src)
mode = stat.S_IMODE(st.st_mode)
if hasattr(os, 'utime'):
os.utime(dst, (st.st_atime, st.st_mtime))
if hasattr(os, 'chmod'):
os.chmod(dst, mode)
if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
try:
os.chflags(dst, st.st_flags)
except OSError, why:
if (not hasattr(errno, 'EOPNOTSUPP') or
why.errno != errno.EOPNOTSUPP):
raise
def copy2(src, dst):
"""Copy data and all stat info ("cp -p src dst").
The destination may be a directory.
"""
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
copyfile(src, dst)
copystat(src, dst)
def emptycopy(src, dst):
"""Create empty file with src name.
Copy file, when file extension in files=[]."""
#1. Markierung
files = ["txt", "py", "pdf", "doc", "odt"]
open(dst, "w").close()
for i in files:
if src[-3:] == i:
copy2(src, dst)
elif src[-2:] == i:
copy2(src, dst)
def copytree(src, dst, symlinks=False, ignore=None):
"""Recursively copy a directory tree with empty files using emptycopy().
"""
names = os.listdir(src)
if ignore is not None:
ignored_names = ignore(src, names)
else:
ignored_names = set()
os.makedirs(dst)
errors = []
for name in names:
if name in ignored_names:
continue
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
copytree(srcname, dstname, symlinks, ignore)
else:
# Will raise a SpecialFileError for unsupported file types
#2. Markierung
emptycopy(srcname, dstname)
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error, err:
errors.extend(err.args[0])
except EnvironmentError, why:
errors.append((srcname, dstname, str(why)))
try:
copystat(src, dst)
except OSError, why:
if WindowsError is not None and isinstance(why, WindowsError):
# Copying file access times may fail on Windows
pass
else:
errors.extend((src, dst, str(why)))
if errors:
raise Error, errors
[/size]
Benutze Win 7 -> emptycopy erstellt versteckte Dateien.