Librería shutil (Operaciones de alto nivel sobre archivos)#

  • Última modificación: Mayo 14, 2022

[1]:
import shutil
[2]:
!touch /tmp/demo.txt
shutil.copyfile('/tmp/demo.txt', '/tmp/demo-copy.txt')
[2]:
'/tmp/demo-copy.txt'
[3]:
!ls -1 /tmp/*.txt
'/tmp/archivo-con-nombre-escapado-*-?.txt'
/tmp/demo-copy.txt
/tmp/demo.txt
/tmp/demo2.txt
/tmp/demo3.txt
[4]:
shutil.copy('/tmp/demo.txt', '/tmp/demo2.txt')
[4]:
'/tmp/demo2.txt'
[5]:
import os

!rm -rf
os.makedirs('/tmp/branch-0/branch-1/branch-2', exist_ok=True)
!touch /tmp/branch-0/branch-1/branch-2.txt
[6]:
shutil.copytree('/tmp/branch-0/', '/tmp/copy')
[6]:
'/tmp/copy'
[7]:
!ls -R /tmp/copy
/tmp/copy:
branch-0-0  branch-1

/tmp/copy/branch-0-0:
branch-0-0-0  branch-0-0-1

/tmp/copy/branch-0-0/branch-0-0-0:
demo-0.txt

/tmp/copy/branch-0-0/branch-0-0-1:
demo-1.txt

/tmp/copy/branch-1:
branch-2  branch-2.txt

/tmp/copy/branch-1/branch-2:
[8]:
shutil.rmtree('/tmp/copy')
[9]:
!ls -1 /tmp
'archivo-con-nombre-escapado-*-?.txt'
branch-0
branch-1
demo
demo-copy.txt
demo.txt
demo2.txt
demo3.txt
[10]:
shutil.move('/tmp/demo2.txt', '/tmp/demo3.txt')
!ls -1 /tmp
'archivo-con-nombre-escapado-*-?.txt'
branch-0
branch-1
demo
demo-copy.txt
demo.txt
demo3.txt
[11]:
shutil.which('python3')
[11]:
'/usr/bin/python3'