thisduck

New Python Script

Aug 8th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. #! python3
  2. # newpy.py creates a new python file with corresponding batch file
  3.  
  4. import os, sys
  5. from pathlib import Path
  6. from subprocess import Popen
  7.  
  8. #preferred py file editor
  9. editor = Path('c:/') / 'Program Files' / 'Notepad++' / 'notepad++.exe'
  10. #preferred py files directory
  11. folder = Path.home() / 'PYTHON'
  12.  
  13. def newpy(name, extension='py', path=folder):
  14.     os.chdir(folder)
  15.     if os.path.exists(name + '.' + extension):
  16.         return print(f"WARNING: No file created.\n{name}.{extension} file already exists in this directory.")
  17.     else:
  18.         newFile = open(name + '.' + extension, 'a')
  19.         newFile.write(f"#! python3\n# {name}.{extension} ")
  20.         newFile.close()
  21.         return print(f"'{name}.{extension}' created at {folder}.")
  22.  
  23. def newbat(name, extension='py'):
  24.     os.chdir(Path.home())
  25.     if os.path.exists(name + '.bat'):
  26.         return print("WARNING: No file created.\nA batch file with this name already exists in this directory.")
  27.     else:
  28.         newFile = open(name + '.bat', 'a')
  29.         newFile.write(f"@{extension}.exe {folder}\\{name}.{extension} %*")
  30.         if extension == 'py':
  31.             newFile.write('\n@pause')
  32.         newFile.close()
  33.         return print(f"'{name}.bat' created for {folder}\\{name}.{extension}.")
  34.  
  35. #invalid arguments
  36. if len(sys.argv) == 1:
  37.     print("ERROR: No file created.\nThis script requires an argument for the new python file name.\nFor more information enter 'help' as first argument.")
  38. elif len(sys.argv) > 3:
  39.     print("ERROR: No file created.\nThis script only accepts up to two arguments.\nFor more information enter 'help' as first argument.")
  40. elif sys.argv[1] == 'help':
  41.     print(r"""DOCUMENTATION FOR newpy.py
  42. Author: thisduck (August 2020) u/thisduck_
  43.  
  44. This script generates a new python file with opening shebang line and second line filename, along with a corresponding batch file for easy launching of the new script.  Two arguments are accepted, but only the first is required.
  45.  
  46. Argument 1: New python script filename
  47.  
  48. Argument 2: File type (either 'py' or 'pyw')
  49. - optional;
  50. - omitting argument defaults to 'py'.
  51. """)
  52.  
  53. #main execution
  54. else: #acceptable inputs
  55.     filename = sys.argv[1]
  56.     if len(sys.argv) > 2:
  57.         if sys.argv[2] != 'py' and sys.argv[2] != 'pyw':
  58.             print(f"CAUTION: Your chosen {sys.argv[2]} file extension may have unexpected results.")
  59.         pytype = sys.argv[2]
  60.        
  61.         #write files
  62.         newpy(filename, pytype)
  63.         newFilePath = folder / f"{filename}.{pytype}"
  64.         newbat(filename, pytype)
  65.     else: #with default extension
  66.         newpy(filename)
  67.         newFilePath = folder / f"{filename}.py"
  68.         newbat(filename)
  69.  
  70. #open new file in editor
  71. Popen(f"{editor} {newFilePath}")
Add Comment
Please, Sign In to add comment