Accepting parameters from the command line with Python


/ Published in: Python
Save to your folder(s)

Accepting parameters from the command line with Python / Aceptando parámetros desde la linea de comandos con Python


Copy this code and paste it in your HTML
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # <Application to hide files on Unix systems.>
  5. # Copyright (C) 2011 Alejandro Alvarez <[email protected]>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19.  
  20. # http://www.codigopython.com.ar <[email protected]>
  21.  
  22. import os, sys
  23.  
  24. def hide(file):
  25. if not os.path.exists(file):
  26. return
  27. archive = os.path.basename(file)
  28. directorio = os.path.dirname(file)
  29. if archive[0] == '.':
  30. return
  31. else:
  32. newfile = directorio+'/''.'+archive
  33. os.rename(file, newfile)
  34.  
  35. def show(file):
  36. if not os.path.exists(file):
  37. return
  38. archive = os.path.basename(file)
  39. directorio = os.path.dirname(file)
  40. if archive[0] != '.':
  41. return
  42. else:
  43. while archive[0] == '.':
  44. archive = archive[1:]
  45. newfile = directorio+'/'+archive
  46. os.rename(file, newfile)
  47.  
  48. def check(file):
  49. if not os.path.exists(file):
  50. return "File does not exist"
  51. archive = os.path.basename(file)
  52. if archive[0] == '.':
  53. return archive + " Is hidden"
  54. else:
  55. return archive + " Is not hidden"
  56.  
  57. if __name__ == '__main__':
  58. if len(sys.argv) != 3:
  59. print "The number of arguments entered is not correct"
  60. file = sys.argv[1]
  61. action = sys.argv[2]
  62. if action == '-c':
  63. print check(file)
  64. elif action == '-h':
  65. hide(file)
  66. print "The file is hidden"
  67. elif action == '-s':
  68. show(file)
  69. print "The file is not hidden"
  70. else:
  71. print "The argument entered is incorrect."

URL: http://wiki.codigopython.com.ar/pyhide:aceptando-parametros-desde-la-linea-de-comandos

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.