thisduck

New Py for JPK

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