advance_touch.py 912 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Libraries
  4. import os
  5. import click
  6. @click.command()
  7. @click.argument('paths', nargs=-1)
  8. @click.option('-cd/--change', is_flag=True, default=False, help='After creating the directories, change to the new deeper directory.')
  9. def advance_touch(paths, cd):
  10. """ Make folders and files """
  11. for path in paths:
  12. # Make folders
  13. new_dirs = '/'.join(path.split('/')[0:-1])
  14. if not os.path.exists(new_dirs) and new_dirs != '':
  15. os.makedirs(new_dirs)
  16. # Change directory
  17. if cd:
  18. cd_path = os.path.join(os.getcwd(), new_dirs) + '/'
  19. os.chdir(cd_path)
  20. # Make file
  21. if not path.endswith('/') and not os.path.isfile(path):
  22. try:
  23. open(path, 'w+').close()
  24. except IsADirectoryError:
  25. pass
  26. if __name__ == '__main__':
  27. advance_touch()