Seite 1 von 1

Kopieren von Datein

Verfasst: Montag 15. April 2013, 19:13
von RSMVDL
Hallo,

Ich brauche nochmal eure Hilfe.
Ich müsste Alle Dateien welche sich innerhalb des Ordners
"source_path" befinden in den Ordner "full_path" kopieren...
wie mach ich das am besten?
Soweit bin ich schon mal!

Code: Alles auswählen

__author__ = 'Robin'
import os
from tempfile import mktemp


def main():
    source_path = input('Path to your files: ')
    mount_point = mktemp(prefix='tmp_iso_', dir='/media')
    os.mkdir(mount_point)
    full_path=(mount_point)
    

if __name__ == '__main__':
    main()

liebe grüße

Re: Kopieren von Datein

Verfasst: Montag 15. April 2013, 19:42
von BlackJack
@RSMVDL: Erklär mal bitte was `full_path` für einen Sinn hat. Wenn Dir der Name `mount_point` nicht gefällt, dann verwende *dort* halt einen anderen Namen, aber führ nicht mehrere Namen für das selbe Objekt ein.

Funktionen zum kopieren von Dateien findet man im `shutil`-Modul.

Re: Kopieren von Datein

Verfasst: Montag 15. April 2013, 20:39
von RSMVDL
Ach lass mir meinen Schwachsinn ;)
Das mit shutil raff ich noch nicht so ganz.
zumindest nicht nach dieser Erklärung
http://openbook.galileocomputing.de/pyt ... 17_007.htm
Ich muss alle Datein und unterordner von A nach B kopieren.
Hat da vllt. jemand einen beispielcode?

Re: Kopieren von Datein

Verfasst: Montag 15. April 2013, 20:52
von BlackJack
@RSMVDL: Man sollte ja auch kein schlechtes Buch sondern die offizielle Dokumentation verwenden.

Re: Kopieren von Datein

Verfasst: Montag 15. April 2013, 21:24
von RSMVDL
So ich hab jetzt folgenden Code:

Code: Alles auswählen

__author__ = 'Robin'
import os,shutil
from subprocess import call
from tempfile import mktemp

def main():
    source_path = input('Path to source files: ')
    final_path = mktemp(prefix='tmp_iso_', dir='/media')
    os.mkdir(final_path)
    shutil.copytree(source_path, final_path, symlinks=False, ignore=None)

if __name__ == '__main__':
    main()

und diese Fehlermeldung unter Ubuntu 12.04 Server 64 bit:

Code: Alles auswählen

root@RepoCloud:~# python3 repo.py
Path to source files: /root/images/ubuntu 12.04
Traceback (most recent call last):
  File "repo.py", line 13, in <module>
    main()
  File "repo.py", line 10, in main
    shutil.copytree(source_path, final_path, symlinks=False, ignore=None)
  File "/usr/lib/python3.2/shutil.py", line 201, in copytree
    os.makedirs(dst)
  File "/usr/lib/python3.2/os.py", line 152, in makedirs
    mkdir(name, mode)
OSError: [Errno 17] File exists: '/media/tmp_iso_kayn4t'
Was mach ich hier falsch!

Re: Kopieren von Datein

Verfasst: Montag 15. April 2013, 21:31
von cofi
RSMVDL hat geschrieben:Was mach ich hier falsch!
Du hast die Dokumentation nicht (genuegend) gelesen:
http://docs.python.org/2/library/shutil.html#shutil.copytree hat geschrieben:The destination directory, named by dst, must not already exist; it will be created as well as missing parent directories.

Re: Kopieren von Datein

Verfasst: Montag 15. April 2013, 21:34
von xeike
Hi!

shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)
Recursively copy an entire directory tree rooted at src, returning the destination directory. The destination directory, named by dst, must not already exist;
Xe

Re: Kopieren von Datein

Verfasst: Montag 15. April 2013, 21:35
von xeike
cofi hat geschrieben:Du hast die Dokumentation nicht (genuegend) gelesen:
Da warst du wohl schneller als ich ;-)

Xe

Re: Kopieren von Datein

Verfasst: Montag 15. April 2013, 21:42
von RSMVDL
Ja jut aber ich erstelle ja vorher in der Funktion main() einen Ordner welcher zufällig benannt wird.
wie soll ich das denn dann bitte zusammen wixxen wenn der Ordner vorher nicht existieren darf!

Aktueller Code:

Code: Alles auswählen

__author__ = 'Robin'
import os,shutil,errno
from tempfile import mktemp
from shutil import copytree, ignore_patterns

def main():
    source_path = input('Path to source files: ')
    final_path = mktemp(prefix='tmp_iso_', dir='/media')
    os.mkdir(final_path)

##Ab hier nur Beispiel. Würde das so evtl. laufen?
def copyanything(source_path, final_path):
    try:
        shutil.copytree(source_path, final_path)
    except OSError as exc:
        if exc.errno == errno.ENOTDIR:
            shutil.copy(source_path, final_path)
        else: raise

if __name__ == '__main__':
    main()

Re: Kopieren von Datein

Verfasst: Montag 15. April 2013, 21:51
von BlackJack
@RSMVDL: Dann erstell den Ordner vorher einfach *nicht*. :roll:

Re: Kopieren von Datein

Verfasst: Montag 15. April 2013, 21:59
von RSMVDL
Aber genau da müssen die Daten ja rein!
Ich muss später mit der Variable final_path weiter arbeiten.
:K
Ich bin ein ziemlich schlechter coder. daher stell ich hier auch solche Fragen ;)

Also wenn du eine Idee hast oder ein Code Beispiel wie ich das hier:

Code: Alles auswählen

__author__ = 'Robin'
import os,shutil,errno
from tempfile import mktemp
from shutil import copytree, ignore_patterns

def main():
    source_path = input('Path to source files: ')
    final_path = mktemp(prefix='tmp_iso_', dir='/media')
    os.mkdir(final_path)

if __name__ == '__main__':
    main()
So hin bekomme das er mir alles von source_path zum final_path kopiert dann wäre ich euch sehr dankbar!

Re: Kopieren von Datein

Verfasst: Montag 15. April 2013, 22:26
von Sirius3
ohne Kommentar:

Code: Alles auswählen

import shutil
from tempfile import mktemp

def main():
    source_path = input('Path to source files: ')
    final_path = mktemp(prefix='tmp_iso_', dir='/media')
    shutil.copytree(source_path, final_path)

if __name__ == '__main__':
    main()

Re: Kopieren von Datein

Verfasst: Montag 15. April 2013, 23:22
von RSMVDL
Ehrlich... DU BIST DER BESTE!