teslariu

Python scripting

Jul 4th, 2023
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.13 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Python 3.10.5 (tags/v3.10.5:f377153, Jun  6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
  5. Type "help", "copyright", "credits" or "license" for more information.
  6. >>> import os
  7. >>> import shutil
  8. >>> os.getcwd()
  9. 'C:\\Users\\Usuario\\AppData\\Local\\Programs\\Python\\Python310'
  10. >>> os.listdir()
  11. ['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']
  12. >>> os.path.exists("C:/Users")
  13. True
  14. >>> os.path.exists("D:/Users")
  15. False
  16. >>> os.chdir("C:/Users/Usuario/Desktop")
  17. >>> os.getcwd()
  18. 'C:\\Users\\Usuario\\Desktop'
  19. >>> os.mkdir("BASURA")
  20. >>> os.listdir()
  21. ['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__']
  22. >>> os.rmdir("BASURA")
  23. >>> os.listdir()
  24. ['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__']
  25. >>> os.mkdir("Ale")
  26. Traceback (most recent call last):
  27.  File "<stdin>", line 1, in <module>
  28. FileExistsError: [WinError 183] No se puede crear un archivo que ya existe: 'Ale'
  29. >>> os.rmdir("Ale")
  30. Traceback (most recent call last):
  31.  File "<stdin>", line 1, in <module>
  32. OSError: [WinError 145] El directorio no está vacío: 'Ale'
  33. >>> os.rename("bubble.xlsx","burbuja.xlsx")
  34. >>> os.listdir()
  35. ['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__']
  36. >>> os.makedirs("2023/Saldos/Enero")
  37. >>> os.rmdir("2023")
  38. Traceback (most recent call last):
  39.  File "<stdin>", line 1, in <module>
  40. OSError: [WinError 145] El directorio no está vacío: '2023'
  41. >>> shutil.rmtree("2023")
  42. >>> os.listdir()
  43. ['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__']
  44. >>> shutil.copy("burbuja.xlsx", "bubble.xlsx")
  45. 'bubble.xlsx'
  46. >>> os.listdir()
  47. ['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__']
  48. >>> shutil.move("MUESTRA.xlsx","test.xlsx")
  49. 'test.xlsx'
  50. >>> os.listdir()
  51. ['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__']
  52. >>>
  53. >>>
  54. >>>
  55. >>> os.path.abspath("test.xlsx")
  56. 'C:\\Users\\Usuario\\Desktop\\test.xlsx'
  57. >>> os.path.dirname("test.xlsx")
  58. ''
  59. >>> os.path.dirname('C:\\Users\\Usuario\\Desktop\\test.xlsx')
  60. 'C:\\Users\\Usuario\\Desktop'
  61. >>> os.path.basename('C:\\Users\\Usuario\\Desktop\\test.xlsx')
  62. 'test.xlsx'
  63. >>> os.path.exists('C:\\Users\\Usuario\\Desktop\\test.xlsx')
  64. True
  65. >>> os.path.exists('C:\\Users\\Usuario\\Desktop\\test22.xlsx')
  66. False
  67. >>> os.path.getsize('C:\\Users\\Usuario\\Desktop\\test.xlsx')
  68. 4884
  69. >>> os.path.isfile('C:\\Users\\Usuario\\Desktop\\test.xlsx')
  70. True
  71. >>> os.path.isdir('C:\\Users\\Usuario\\Desktop\\test.xlsx')
  72. False
  73. >>>
  74.  
  75.  
  76. # como leer argumentos desde consola: sys.argv
  77. # Script que saluda
  78. import sys
  79.  
  80. nombres = sys.argv[1:]
  81. if nombres:
  82.    for nombre in nombres:
  83.        print(f"Hola {nombre}")
  84. else:
  85.    print("Hola desconocido, no me has dicho tu nombre")
  86. """
  87.  
  88. # Script que pide una ruta y una extensión y devuelve el total de archivos
  89. # hallados y una lista de los mismos CON RECURSIVIDAD
  90. # Ej:
  91. # python buscar_archivos.py <ruta> <extension>
  92.  
  93.  
  94. import os
  95. import sys
  96.  
  97. # variable que devuelve la cantidad de archivos hallados
  98. hallados = 0
  99.  
  100. # verifico la cantidad de argumentos
  101. if len(sys.argv) != 3:
  102.     sys.exit("Error: ejecute 'python buscar_archivos.py <ruta> <extension>'")
  103.  
  104. # asigno los argumentos
  105. ruta, extension = sys.argv[1:]
  106.  
  107. # verifico la ruta
  108. if not os.path.exists(ruta):
  109.     sys.exit(f"No existe la ruta '{ruta}'")
  110.    
  111. # busco los archivos
  112. # usaré os.walk() que usa 3 iteradores. En cada iteración devuelve
  113. # a) El nombre del directorio actual
  114. # b) una lista con los subdirectorios dentro del directorio actual
  115. # c) una lista con los archivos dentro del directorio actual
  116.  
  117. for dir_actual, lista_subdirs, lista_archivos in os.walk(ruta):
  118.     for archivo in lista_archivos:
  119.         if archivo.endswith(extension):
  120.             print(os.path.join(dir_actual,archivo))
  121.             hallados += 1
  122.            
  123. if hallados:
  124.     print(f"Total de archivos: {hallados}")
  125. else:
  126.     print(f"No se hallaron archivos con extension '{extension}' en la ruta '{ruta}'")
Add Comment
Please, Sign In to add comment