我正在用python 3开发一个脚本,该脚本读取用户的samba配置文件和共享文件夹的路径。脚本必须将共享文件夹备份到USB设备。
我在复制文件夹时遇到问题,因为我的代码停留在路径列表的第一条路径上
编辑1
问题似乎出在:
for path in paths:
#Se reemplaza el string ruta por la ruta completa hasta los archivos sin 'path = '
path = path.replace('path = ', '')
#Ruta del directorio (carpeta compartida)
dirname = os.path.dirname(os.path.abspath(path))
#Nombre de la carpeta contenedora (carpeta compartida)
basename = os.path.basename(path)
print(path + ' respaldado en ' + usb_path + '/' + basename)
shutil.copytree(path, usb_path + '/' + basename)
return dirname
编辑2
代码如下:
backup.py
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 5 08:40:56 2019
@author: Juan Carlos Reyes Suazo
"""
import shutil
import os
class backup:
archive = input('Escriba la ruta de su archivo smb.conf: ')
usb_path = input('Escriba la ruta de su dispositivo USB: ')
#Metodo que lee el archivo de texto smb.conf, guarda los usuarios y las rutas y copia las carpetas de dichas rutas en el USB
def read_file_copy_backup(archive, usb_path):
#Abre el archivo y lo lee
with open(archive) as file:
#Busca la linea 9610
file.seek(9610)
tuple_list = list()
#Recorre el archivo linea por linea
for line in file:
#Separa las lineas
line = line.strip()
if not line:
continue
#Separa la linea nuevamente para quitar los [] y anade el elemento a la lista de lineas
tuple_list.append(line)
# print(tuple_list)
# Crea tuplas users y paths
users = list()
paths= list()
# #Recorre la tupla de lineas
for element in tuple_list:
#Si el elemento empieza con '[' se anade a la tupla users
if element.startswith('['):
users.append(element)
#Si el elemento empieza con 'path = ' se reemplaza 'path = ' por '' y se anade a la tupla paths
if element.startswith('path = '):
paths.append(element)
for path in paths:
#Se reemplaza el string ruta por la ruta completa hasta los archivos sin 'path = '
path = path.replace('path = ', '')
#Ruta del directorio (carpeta compartida)
dirname = os.path.dirname(os.path.abspath(path))
#Nombre de la carpeta contenedora (carpeta compartida)
basename = os.path.basename(path)
print(path + ' respaldado en ' + usb_path + '/' + basename)
shutil.copytree(path, usb_path + '/' + basename)
return dirname
read_file_copy_backup(archive, usb_path)
从9610行到文件结尾的smb.conf文件
[prueba]
comment = prueba
path = /home/servidor/Documentos/prueba
writeable = yes
; browseable = yes
valid users = servidorftp
[Compartido-proyectos]
path = /home/fsoto/Documentos/Compartido-proyectos
writeable = yes
; browseable = yes
valid users = aigor, fsoto, pbarriento, rsolis
[marearoja]
comment = marearoja
path = /home/marearoja/Documentos/
writeable = yes
; browseable = yes
valid users = labpm, marearoja
[finanzas]
comment = finanzas
path = /home/finanzas/Documentos
writeable = yes
; browseable = yes
valid users = finanzas
[Auditoria]
comment = auditoria
path = /home/auditores/Documentos
writeable = yes
; browseable = yes
valid users = auditores
[saludocupacional]
comment = saludocupacional
path = /home/saludocupacional/Documentos/saludocupacional
writeable = yes
; browseable = yes
valid users = hcordero, saludocupacional
[docsOsorno]
comment = documentos
path = /home/osorno/Documentos/documentos
writeable = yes
; browseable = yes
valid users = osorno
[profmed]
comment = profmed
path = /home/profmed/Documentos/profmed
writeable = yes
; browseable = yes
valid users = profmed
[ugp]
comment = ugp
path = /home/ugp/Documentos/ugp
writeable = yes
; browseable = yes
valid users = ugp
[CONTROL DE GESTION]
comment = CONTROL DE GESTION
path = /home/controlgestion/Documentos/CONTROL DE GESTION
writeable = yes
; browseable = yes
valid users = controlgestion
[tabaco]
comment = tabaco
path = /home/tabaco/Documentos/tabaco
writeable = yes
; browseable = yes
valid users = tabaco
[formalizaciones]
comment = formalizaciones
path = /home/formalizaciones/Documentos/formalizaciones
writeable = yes
; browseable = yes
valid users = formalizaciones
[alimentos]
path = /home/alimentos/Documentos/alimentos
writeable = yes
; browseable = yes
valid users = alimentos
comment = alimentos
[exenciones de arancel]
comment = exenciones de arancel
path = /home/arancel/Documentos/exenciones de arancel
writeable = yes
; browseable = yes
valid users = arancel, oirsch, servidorftp
[proyectos de sentencia chiloe]
comment = proyectos de sentencia chiloe
path = /home/sumarios/Documentos/proyectos de sentencia chiloe
writeable = yes
; browseable = yes
valid users = servidorftp, sumarios
[programa intercultural]
comment = programa intercultural
path = /home/intercultural/Documentos/programa intercultural
writeable = yes
; browseable = yes
valid users = intercultural, promotorsalud, promoyparticipacion, saludpublica
[Compartido-proyectos]
path = /home/fsoto/Documentos/Compartido-proyectos
writeable = yes
; browseable = yes
valid users = aigor, fsoto, pbarriento, rsolis
[licencias medicas]
comment = licencias medicas
path = /home/licmed/Documentos/licencias medicas
writeable = yes
; browseable = yes
valid users = licmed
[Cumplimiento y evaluacion AGUAS Programa 2015]
comment = Cumplimiento y evaluacion AGUAS Programa 2015
path = /home/aguas/Documentos/Cumplimiento y evaluacion AGUAS Programa 2015
writeable = yes
; browseable = yes
valid users = aguachiloe, aguaosorno, aguapalena, aguapmontt, salud_ambiental, unidadaguachiloe
[sgc]
comment = sgc
path = /home/sgc/Documentos/sgc
writeable = yes
; browseable = yes
valid users = alimentoscomp2017, formalizacion, sgc
[respaldos]
path = /media/servidorftp/respaldo/respaldos
writeable = yes
browseable = no
valid users = servidorftp
comment = respaldos
[entel]
comment = entel
path = /home/entel/Documentos/entel
writeable = yes
; browseable = yes
valid users = entel, rmunoz, servidorftp
[depto dsp]
comment = depto dsp
path = /home/dsp/Documentos/depto dsp
writeable = yes
; browseable = yes
valid users = dsp, promotorsalud, promoyparticipacion, saludpublica
[videosSeremi]
comment = videosSeremi
path = /home/servidorftp/videosSeremi
; writeable = No
browseable = no
guest ok = yes
[uso]
comment = uso-chiloe
path = /home/uso-chiloe/Documentos/uso
writeable = yes
; browseable = yes
valid users = uso-chiloe
[Documentos]
path = /home/discapacidad-chiloe/Documentos
writeable = yes
; browseable = yes
valid users = discapacidad-chiloe
[discapacidadchiloe]
comment = discapacidadchiloe
path = /home/discapacidad-chiloe/Documentos
writeable = yes
; browseable = yes
valid users = discapacidad-chiloe
[pmg]
comment = pmg
path = /home/pmg/Documentos/pmg
writeable = yes
; browseable = yes
valid users = pmg
[Emergencia marea roja Osorno]
comment = Emergencia marea roja Osorno
path = /home/mro/Documentos/Emergencia marea roja Osorno
writeable = yes
; browseable = yes
valid users = mro
[Deis]
comment = Deis
path = /home/deis/Documentos/Deis
writeable = yes
; browseable = yes
valid users = deis
[labpm]
comment = labpm
path = /home/labpm/Documentos/labpm
writeable = yes
; browseable = yes
valid users = labpm, labpml
[abastecimiento]
comment = abastecimiento
path = /home/abastecimiento/Documentos/abastecimiento
; writeable = No
; browseable = yes
guest ok = yes
[rrhh]
comment = rrhh
path = /home/rrhh/Documentos/rrhh
; writeable = No
; browseable = yes
guest ok = yes
[cmpregional]
comment = cmpregional
path = /home/compin/Documentos/cmpregional
writeable = yes
; browseable = yes
valid users = compin
[dspchiloe]
comment = dspchiloe
path = /home/dspchiloe/Documentos/dspchiloe
writeable = yes
; browseable = yes
valid users = dspchiloe, laboratoriocastro
[monitoreo_osorno]
comment = monitoreo_osorno
path = /home/monitoreos/Documentos/monitoreo_osorno
writeable = yes
; browseable = yes
valid users = monitoreos
[residuos llanquihue]
comment = residuos llanquihue
path = /home/residuos/Documentos/residuos llanquihue
writeable = yes
; browseable = yes
valid users = aguachiloe, aguapmontt, aigor, fsoto, hcordero, residuos, rparra
[Formalizacion OirsPm]
comment = Formalizacion OirsPm
path = /home/formalizacion/Documentos/Formalizacion OirsPm
writeable = yes
; browseable = yes
valid users = formalizacion
[promoyparticipacion]
comment = promoyparticipacion
path = /home/promoyparticipacion/Documentos/promoyparticipacion
writeable = yes
; browseable = yes
valid users = dsp, eupalimentos, promoyparticipacion
[test]
comment = test
path = /home/test/Documentos/test
writeable = yes
; browseable = yes
valid users = test
[alimentoscomp2017]
path = /home/alimentoscomp2017/Documentos/alimentoscomp2017
writeable = yes
; browseable = yes
valid users = alimentoscomp2017, proyectoosorno, sgc
comment = alimentoscomp2017
[EUPALIMENTOPROMOCION]
path = /home/eupalimentos/Documentos/EUPALIMENTOPROMOCION
writeable = yes
; browseable = yes
valid users = eupalimentos, promoyparticipacion, sgc
comment = eupalimentopromocion
[promoyparticipacionl]
comment = promoyparticipacionl
path = /home/promoyparticipacion/Documentos/promoyparticipacion
writeable = yes
; browseable = yes
valid users = eup, promotorsalud
[FiebreQ]
path = /home/fiebreq/Documentos/FiebreQ
writeable = yes
; browseable = yes
valid users = fiebreq
[respaldos-cobian]
comment = respaldos-cobian
path = /media/servidorftp/respaldo/respaldos-cobian
; writeable = No
; browseable = yes
valid users = servidorftp
[Unidad Agua Chiloe]
path = /home/unidadaguachiloe/Documentos/Unidad Agua Chiloe
writeable = yes
; browseable = yes
valid users = aguachiloe, unidadaguachiloe
[Seremi]
comment = seremi
path = /home/seremi/Documentos/Seremi
writeable = yes
; browseable = yes
valid users = seremi
[ugp formatos]
comment = ugp formatos
path = /home/ugp/Documentos/ugp formatos
writeable = yes
; browseable = yes
valid users = ugp
[ugp formatos visita]
comment = ugp formatos visita
path = /home/ugp/Documentos/ugp formatos
; writeable = No
; browseable = yes
guest ok = yes
[saludambiental]
comment = saludambiental
path = /home/salud_ambiental/Documentos/planilla
writeable = yes
; browseable = yes
valid users = aguapmontt, rparra, salud_ambiental, sgc
[EUP]
path = /home/eup/Documentos/EUP
writeable = yes
; browseable = yes
valid users = eup, promoyparticipacion, salud_ambiental
[EUPL]
comment = EUPL
path = /home/eup/Documentos/EUP
; writeable = No
; browseable = yes
valid users = saludocupacional, usuariolectura
[saludambientalL]
comment = saludambientalL
path = /home/salud_ambiental/Documentos
; writeable = No
; browseable = yes
valid users = usuariolectura
[labcastro]
comment = labcastro
path = /home/laboratoriocastro/Documentos/labcastro
writeable = yes
; browseable = yes
valid users = dspchiloe, laboratoriocastro
[oirschiloe]
comment = oirschiloe
path = /home/oirsch/Documentos/oirschiloe
writeable = yes
; browseable = yes
valid users = oirsch, servidorftp
[Sumarios Sanitarios Juridica]
comment = Sumarios Sanitarios juridica
path = /home/sumariosjur/Documentos/Sumarios Sanitarios Juridica
writeable = yes
; browseable = yes
valid users = servidorftp, sumariosjur
[OirsQuellon]
comment = OirsQuellon
path = /home/oirsquellon/Documentos/OirsQuellon
writeable = yes
; browseable = yes
valid users = oirsquellon
[OirsAncud]
comment = OirsAncud
path = /home/oirsancud/Documentos/OirsAncud
writeable = yes
; browseable = yes
valid users = oirsancud
[Discapacidad Compin PM]
comment = Discapacidad Compin PM
path = /home/compinpm/Documentos/Discapacidad Compin PM
writeable = yes
; browseable = yes
valid users = compinpm
[EPP]
comment = EPP
path = /home/epp/Documentos/EPP
writeable = yes
; browseable = yes
valid users = epp
[gabinete]
comment = gabinete
path = /home/gabinete/Documentos/gabinete
writeable = yes
; browseable = yes
valid users = gabinete
[informatica]
comment = informatica
path = /media/servidorftp/imagen/informatica
writeable = yes
; browseable = yes
valid users = servidorftp
[Proyecto Osorno]
comment = Proyecto Osorno
path = /home/proyectoosorno/Documentos/Proyecto Osorno
writeable = yes
; browseable = yes
valid users = alimentoscomp2017, proyectoosorno
[comite paritario chiloe]
comment = comite paritario chiloe
path = /home/cphysc/Documentos/comite paritario chiloe
writeable = yes
; browseable = yes
valid users = cphysc, oirsch, servidorftp
[ComiteParitario]
comment = ComiteParitario
path = /home/cparitario/Documentos/ComiteParitario
writeable = yes
; browseable = yes
valid users = compinpm, cparitario
[COMUNICACIONES]
comment = COMUNICACIONES
path = /home/comunicaciones/Documentos/COMUNICACIONES
writeable = yes
; browseable = yes
valid users = comunicaciones
[soportetic]
comment = soportetic
path = /home/soportetic/Documentos/soportetic
writeable = yes
; browseable = yes
valid users = servidorftp, soportetic
[ReunionesSeremi]
comment = ReunionesSeremi
path = /home/reuseremi/Documentos/ReunionesSeremi
writeable = yes
; browseable = yes
valid users = reuseremi
[SeguridadLaboratoriosAmbientales]
comment = SeguridadLaboratoriosAmbientales
path = /home/seguridadlabam/Documentos/SeguridadLaboratoriosAmbientales
writeable = yes
; browseable = yes
valid users = seguridadlabam
[Profmedx]
comment = Profmedx
path = /home/profmedx/Documentos/Profmedx
writeable = yes
; browseable = yes
valid users = profmedx
[backup-cobian]
path = /media/servidorftp/BACKUP/backup-cobian
; writeable = No
; browseable = yes
valid users = servidorftp, soportetic
[labosorno]
comment = labosorno
path = /home/labosorno/Documentos/labosorno
writeable = yes
; browseable = yes
valid users = labosorno
我正在尝试复制每个文件夹及其在smb.config文件中的内容