Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
- Type "help", "copyright", "credits" or "license" for more information.
- >>> import os
- >>> import shutil
- >>> os.getcwd()
- 'C:\\Users\\Usuario\\AppData\\Local\\Programs\\Python\\Python310'
- >>> os.listdir()
- ['AAABBB', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python310.dll', 'pythonw.exe', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll', 'vcruntime140_1.dll']
- >>> os.path.exists("C:/Users")
- True
- >>> os.path.exists("D:/Users")
- False
- >>> os.chdir("C:/Users/Usuario/Desktop")
- >>> os.getcwd()
- 'C:\\Users\\Usuario\\Desktop'
- >>> os.mkdir("BASURA")
- >>> os.listdir()
- ['1427859-22-01.pdf', 'acme.py', 'acme.spec', 'Ale', 'BASURA', 'BitTorrent.lnk', 'bubble.xlsx', 'Cain.lnk', 'camion2.png', 'DEBUG.exe', 'desktop.ini', 'firma guille luz.jpg', 'Git-2.37.1-64-bit.exe', 'globo.png', 'gpg4win-4.0.3.exe', 'icono.ico', 'icono.svg', 'infostatinstaller_esp.exe', 'MARKETING - AGUS, KANKI CONSTRUCTORA', 'MediaHuman YouTube to MP3 Converter.lnk', 'mibase.sqlite', 'MiClase.mdj', 'MUESTRA.xlsx', 'mv(1).PNG', 'MysqlWorkbench (1).ppt', 'MysqlWorkbench.ppt', 'Nacimientos.sql', 'niClase.mdj', 'nmap-7.93-setup.exe', 'Pedidos_Neptuno.sql', 'pie3D.xlsx', 'plantilla while True.py', 'ProgramaMain.java', 'proyecto 2.zip', 'python-3.11.3-amd64.exe', 'Raj Badhwar (auth.) - The CISO’s Next Frontier_ AI, Post-Quantum Cryptography and Advanced Security Paradigms (2021, Springer) [10.1007_978-3-030-75354-2] - libgen.li.pdf', 'redo.png', 'sin títcxcxvulo.py', 'sin título.py', 'Start Tor Browser.lnk', 'StarUML Setup 5.0.2.exe', 'Tasm_1.4_Windows_7-Windows_8_64_bit_Techapple.exe', 'Tor Browser', 'torbrowser-install-win64-12.0.1_ALL.exe', 'undo.png', 'Visit MediaHuman Website.url', 'Webinar', 'WhatsApp Image 2022-09-03 at 12.45.04.jpeg', 'WinPcap_4_1_3.exe', 'Wireshark-win64-4.0.1.exe', 'xampp-windows-x64-8.2.4-0-VS16-installer.exe', '__pycache__']
- >>> os.rmdir("BASURA")
- >>> os.listdir()
- ['1427859-22-01.pdf', 'acme.py', 'acme.spec', 'Ale', 'BitTorrent.lnk', 'bubble.xlsx', 'Cain.lnk', 'camion2.png', 'DEBUG.exe', 'desktop.ini', 'firma guille luz.jpg', 'Git-2.37.1-64-bit.exe', 'globo.png', 'gpg4win-4.0.3.exe', 'icono.ico', 'icono.svg', 'infostatinstaller_esp.exe', 'MARKETING - AGUS, KANKI CONSTRUCTORA', 'MediaHuman YouTube to MP3 Converter.lnk', 'mibase.sqlite', 'MiClase.mdj', 'MUESTRA.xlsx', 'mv(1).PNG', 'MysqlWorkbench (1).ppt', 'MysqlWorkbench.ppt', 'Nacimientos.sql', 'niClase.mdj', 'nmap-7.93-setup.exe', 'Pedidos_Neptuno.sql', 'pie3D.xlsx', 'plantilla while True.py', 'ProgramaMain.java', 'proyecto 2.zip', 'python-3.11.3-amd64.exe', 'Raj Badhwar (auth.) - The CISO’s Next Frontier_ AI, Post-Quantum Cryptography and Advanced Security Paradigms (2021, Springer) [10.1007_978-3-030-75354-2] - libgen.li.pdf', 'redo.png', 'sin títcxcxvulo.py', 'sin título.py', 'Start Tor Browser.lnk', 'StarUML Setup 5.0.2.exe', 'Tasm_1.4_Windows_7-Windows_8_64_bit_Techapple.exe', 'Tor Browser', 'torbrowser-install-win64-12.0.1_ALL.exe', 'undo.png', 'Visit MediaHuman Website.url', 'Webinar', 'WhatsApp Image 2022-09-03 at 12.45.04.jpeg', 'WinPcap_4_1_3.exe', 'Wireshark-win64-4.0.1.exe', 'xampp-windows-x64-8.2.4-0-VS16-installer.exe', '__pycache__']
- >>> os.mkdir("Ale")
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- FileExistsError: [WinError 183] No se puede crear un archivo que ya existe: 'Ale'
- >>> os.rmdir("Ale")
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- OSError: [WinError 145] El directorio no está vacío: 'Ale'
- >>> os.rename("bubble.xlsx","burbuja.xlsx")
- >>> os.listdir()
- ['1427859-22-01.pdf', 'acme.py', 'acme.spec', 'Ale', 'BitTorrent.lnk', 'burbuja.xlsx', 'Cain.lnk', 'camion2.png', 'DEBUG.exe', 'desktop.ini', 'firma guille luz.jpg', 'Git-2.37.1-64-bit.exe', 'globo.png', 'gpg4win-4.0.3.exe', 'icono.ico', 'icono.svg', 'infostatinstaller_esp.exe', 'MARKETING - AGUS, KANKI CONSTRUCTORA', 'MediaHuman YouTube to MP3 Converter.lnk', 'mibase.sqlite', 'MiClase.mdj', 'MUESTRA.xlsx', 'mv(1).PNG', 'MysqlWorkbench (1).ppt', 'MysqlWorkbench.ppt', 'Nacimientos.sql', 'niClase.mdj', 'nmap-7.93-setup.exe', 'Pedidos_Neptuno.sql', 'pie3D.xlsx', 'plantilla while True.py', 'ProgramaMain.java', 'proyecto 2.zip', 'python-3.11.3-amd64.exe', 'Raj Badhwar (auth.) - The CISO’s Next Frontier_ AI, Post-Quantum Cryptography and Advanced Security Paradigms (2021, Springer) [10.1007_978-3-030-75354-2] - libgen.li.pdf', 'redo.png', 'sin títcxcxvulo.py', 'sin título.py', 'Start Tor Browser.lnk', 'StarUML Setup 5.0.2.exe', 'Tasm_1.4_Windows_7-Windows_8_64_bit_Techapple.exe', 'Tor Browser', 'torbrowser-install-win64-12.0.1_ALL.exe', 'undo.png', 'Visit MediaHuman Website.url', 'Webinar', 'WhatsApp Image 2022-09-03 at 12.45.04.jpeg', 'WinPcap_4_1_3.exe', 'Wireshark-win64-4.0.1.exe', 'xampp-windows-x64-8.2.4-0-VS16-installer.exe', '__pycache__']
- >>> os.makedirs("2023/Saldos/Enero")
- >>> os.rmdir("2023")
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- OSError: [WinError 145] El directorio no está vacío: '2023'
- >>> shutil.rmtree("2023")
- >>> os.listdir()
- ['1427859-22-01.pdf', 'acme.py', 'acme.spec', 'Ale', 'BitTorrent.lnk', 'burbuja.xlsx', 'Cain.lnk', 'camion2.png', 'DEBUG.exe', 'desktop.ini', 'firma guille luz.jpg', 'Git-2.37.1-64-bit.exe', 'globo.png', 'gpg4win-4.0.3.exe', 'icono.ico', 'icono.svg', 'infostatinstaller_esp.exe', 'MARKETING - AGUS, KANKI CONSTRUCTORA', 'MediaHuman YouTube to MP3 Converter.lnk', 'mibase.sqlite', 'MiClase.mdj', 'MUESTRA.xlsx', 'mv(1).PNG', 'MysqlWorkbench (1).ppt', 'MysqlWorkbench.ppt', 'Nacimientos.sql', 'niClase.mdj', 'nmap-7.93-setup.exe', 'Pedidos_Neptuno.sql', 'pie3D.xlsx', 'plantilla while True.py', 'ProgramaMain.java', 'proyecto 2.zip', 'python-3.11.3-amd64.exe', 'Raj Badhwar (auth.) - The CISO’s Next Frontier_ AI, Post-Quantum Cryptography and Advanced Security Paradigms (2021, Springer) [10.1007_978-3-030-75354-2] - libgen.li.pdf', 'redo.png', 'sin títcxcxvulo.py', 'sin título.py', 'Start Tor Browser.lnk', 'StarUML Setup 5.0.2.exe', 'Tasm_1.4_Windows_7-Windows_8_64_bit_Techapple.exe', 'Tor Browser', 'torbrowser-install-win64-12.0.1_ALL.exe', 'undo.png', 'Visit MediaHuman Website.url', 'Webinar', 'WhatsApp Image 2022-09-03 at 12.45.04.jpeg', 'WinPcap_4_1_3.exe', 'Wireshark-win64-4.0.1.exe', 'xampp-windows-x64-8.2.4-0-VS16-installer.exe', '__pycache__']
- >>> shutil.copy("burbuja.xlsx", "bubble.xlsx")
- 'bubble.xlsx'
- >>> os.listdir()
- ['1427859-22-01.pdf', 'acme.py', 'acme.spec', 'Ale', 'BitTorrent.lnk', 'bubble.xlsx', 'burbuja.xlsx', 'Cain.lnk', 'camion2.png', 'DEBUG.exe', 'desktop.ini', 'firma guille luz.jpg', 'Git-2.37.1-64-bit.exe', 'globo.png', 'gpg4win-4.0.3.exe', 'icono.ico', 'icono.svg', 'infostatinstaller_esp.exe', 'MARKETING - AGUS, KANKI CONSTRUCTORA', 'MediaHuman YouTube to MP3 Converter.lnk', 'mibase.sqlite', 'MiClase.mdj', 'MUESTRA.xlsx', 'mv(1).PNG', 'MysqlWorkbench (1).ppt', 'MysqlWorkbench.ppt', 'Nacimientos.sql', 'niClase.mdj', 'nmap-7.93-setup.exe', 'Pedidos_Neptuno.sql', 'pie3D.xlsx', 'plantilla while True.py', 'ProgramaMain.java', 'proyecto 2.zip', 'python-3.11.3-amd64.exe', 'Raj Badhwar (auth.) - The CISO’s Next Frontier_ AI, Post-Quantum Cryptography and Advanced Security Paradigms (2021, Springer) [10.1007_978-3-030-75354-2] - libgen.li.pdf', 'redo.png', 'sin títcxcxvulo.py', 'sin título.py', 'Start Tor Browser.lnk', 'StarUML Setup 5.0.2.exe', 'Tasm_1.4_Windows_7-Windows_8_64_bit_Techapple.exe', 'Tor Browser', 'torbrowser-install-win64-12.0.1_ALL.exe', 'undo.png', 'Visit MediaHuman Website.url', 'Webinar', 'WhatsApp Image 2022-09-03 at 12.45.04.jpeg', 'WinPcap_4_1_3.exe', 'Wireshark-win64-4.0.1.exe', 'xampp-windows-x64-8.2.4-0-VS16-installer.exe', '__pycache__']
- >>> shutil.move("MUESTRA.xlsx","test.xlsx")
- 'test.xlsx'
- >>> os.listdir()
- ['1427859-22-01.pdf', 'acme.py', 'acme.spec', 'Ale', 'BitTorrent.lnk', 'bubble.xlsx', 'burbuja.xlsx', 'Cain.lnk', 'camion2.png', 'DEBUG.exe', 'desktop.ini', 'firma guille luz.jpg', 'Git-2.37.1-64-bit.exe', 'globo.png', 'gpg4win-4.0.3.exe', 'icono.ico', 'icono.svg', 'infostatinstaller_esp.exe', 'MARKETING - AGUS, KANKI CONSTRUCTORA', 'MediaHuman YouTube to MP3 Converter.lnk', 'mibase.sqlite', 'MiClase.mdj', 'mv(1).PNG', 'MysqlWorkbench (1).ppt', 'MysqlWorkbench.ppt', 'Nacimientos.sql', 'niClase.mdj', 'nmap-7.93-setup.exe', 'Pedidos_Neptuno.sql', 'pie3D.xlsx', 'plantilla while True.py', 'ProgramaMain.java', 'proyecto 2.zip', 'python-3.11.3-amd64.exe', 'Raj Badhwar (auth.) - The CISO’s Next Frontier_ AI, Post-Quantum Cryptography and Advanced Security Paradigms (2021, Springer) [10.1007_978-3-030-75354-2] - libgen.li.pdf', 'redo.png', 'sin títcxcxvulo.py', 'sin título.py', 'Start Tor Browser.lnk', 'StarUML Setup 5.0.2.exe', 'Tasm_1.4_Windows_7-Windows_8_64_bit_Techapple.exe', 'test.xlsx', 'Tor Browser', 'torbrowser-install-win64-12.0.1_ALL.exe', 'undo.png', 'Visit MediaHuman Website.url', 'Webinar', 'WhatsApp Image 2022-09-03 at 12.45.04.jpeg', 'WinPcap_4_1_3.exe', 'Wireshark-win64-4.0.1.exe', 'xampp-windows-x64-8.2.4-0-VS16-installer.exe', '__pycache__']
- >>>
- >>>
- >>>
- >>> os.path.abspath("test.xlsx")
- 'C:\\Users\\Usuario\\Desktop\\test.xlsx'
- >>> os.path.dirname("test.xlsx")
- ''
- >>> os.path.dirname('C:\\Users\\Usuario\\Desktop\\test.xlsx')
- 'C:\\Users\\Usuario\\Desktop'
- >>> os.path.basename('C:\\Users\\Usuario\\Desktop\\test.xlsx')
- 'test.xlsx'
- >>> os.path.exists('C:\\Users\\Usuario\\Desktop\\test.xlsx')
- True
- >>> os.path.exists('C:\\Users\\Usuario\\Desktop\\test22.xlsx')
- False
- >>> os.path.getsize('C:\\Users\\Usuario\\Desktop\\test.xlsx')
- 4884
- >>> os.path.isfile('C:\\Users\\Usuario\\Desktop\\test.xlsx')
- True
- >>> os.path.isdir('C:\\Users\\Usuario\\Desktop\\test.xlsx')
- False
- >>>
- # como leer argumentos desde consola: sys.argv
- # Script que saluda
- import sys
- nombres = sys.argv[1:]
- if nombres:
- for nombre in nombres:
- print(f"Hola {nombre}")
- else:
- print("Hola desconocido, no me has dicho tu nombre")
- """
- # Script que pide una ruta y una extensión y devuelve el total de archivos
- # hallados y una lista de los mismos CON RECURSIVIDAD
- # Ej:
- # python buscar_archivos.py <ruta> <extension>
- import os
- import sys
- # variable que devuelve la cantidad de archivos hallados
- hallados = 0
- # verifico la cantidad de argumentos
- if len(sys.argv) != 3:
- sys.exit("Error: ejecute 'python buscar_archivos.py <ruta> <extension>'")
- # asigno los argumentos
- ruta, extension = sys.argv[1:]
- # verifico la ruta
- if not os.path.exists(ruta):
- sys.exit(f"No existe la ruta '{ruta}'")
- # busco los archivos
- # usaré os.walk() que usa 3 iteradores. En cada iteración devuelve
- # a) El nombre del directorio actual
- # b) una lista con los subdirectorios dentro del directorio actual
- # c) una lista con los archivos dentro del directorio actual
- for dir_actual, lista_subdirs, lista_archivos in os.walk(ruta):
- for archivo in lista_archivos:
- if archivo.endswith(extension):
- print(os.path.join(dir_actual,archivo))
- hallados += 1
- if hallados:
- print(f"Total de archivos: {hallados}")
- else:
- print(f"No se hallaron archivos con extension '{extension}' en la ruta '{ruta}'")
Add Comment
Please, Sign In to add comment