We Recommend

Learning Python Learning Python
The authors of Learning Python show you enough essentials of the Python scripting language to enable you to begin solving problems right away, then reveal more powerful aspects of the language one at a time. This approach is sure to appeal to programmers and system administrators who have urgent problems and a preference for learning by semi-guided experimentation.


Posted By

buscarini on 07/13/06


Tagged

ficheros directorios


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

anayhk


Sobreescribir todos los ficheros de cierto nombre en una ruta


Published in: Python 


Este script busca en la ruta que se le pase como argumento todos los ficheros Video.htm, y los sobreescribe con el Video.htm que esté el el mismo directorio que el script

  1. import os
  2. import os.path
  3. import sys
  4. import shutil
  5.  
  6. def copytodir(origin,dir):
  7. files = os.listdir(dir)
  8.  
  9. for file in files:
  10. #file.lower()
  11.  
  12. #print(file)
  13.  
  14. # Si es un video.html sobreescribirlo con el fichero origen
  15. if (file == "Video.htm"):
  16. print("origin: " + origin + " to " + dir + "/" + "Video.htm")
  17.  
  18. shutil.copyfile(origin, dir + "/" + "Video.htm")
  19.  
  20. # Si es un directorio entrar dentro a buscar un video.html
  21. if (os.path.isdir(dir + "/" + file)):
  22. copytodir(origin,dir + "/" + file)
  23.  
  24.  
  25.  
  26. def main(dir):
  27. files = os.listdir(".")
  28. #print (files)
  29. encontrado = False
  30.  
  31. for file in files:
  32. if (file == "Video.htm"):
  33. encontrado = True
  34. if (encontrado):
  35. copytodir("./Video.htm",dir)
  36. else:
  37. print("No he encontrado el archivo origen Video.htm")
  38.  
  39.  
  40.  
  41. if __name__ == "__main__":
  42. main(sys.argv[1])

Report this snippet 

You need to login to post a comment.