Kopieren von Datein

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
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
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.
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?
BlackJack

@RSMVDL: Man sollte ja auch kein schlechtes Buch sondern die offizielle Dokumentation verwenden.
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!
Benutzeravatar
cofi
Python-Forum Veteran
Beiträge: 4432
Registriert: Sonntag 30. März 2008, 04:16
Wohnort: RGFybXN0YWR0

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.
xeike
User
Beiträge: 83
Registriert: Donnerstag 28. Februar 2013, 09:58

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
xeike
User
Beiträge: 83
Registriert: Donnerstag 28. Februar 2013, 09:58

cofi hat geschrieben:Du hast die Dokumentation nicht (genuegend) gelesen:
Da warst du wohl schneller als ich ;-)

Xe
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()
BlackJack

@RSMVDL: Dann erstell den Ordner vorher einfach *nicht*. :roll:
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!
Sirius3
User
Beiträge: 17738
Registriert: Sonntag 21. Oktober 2012, 17:20

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()
RSMVDL

Ehrlich... DU BIST DER BESTE!
Antworten