Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Concept of batch files
#1
I wrote a simple password management program and have a problem to utilize it since I don't quite understand what batch file is and how to use it.
code with filename pw.py:
#! python3
# pw.py  - An insecure password locker program

PASSWORDS = {'yahoo': 'alpha',
             'linkedin': 'beta',
             'youtube': 'beta',
             'job': 'gama',
			 'python-io': 'delta' }

			 
import sys, pyperclip
if len(sys.argv) < 2:
	print('Usage: python pw.py [account] - copy account password')
	sys.exit()
	
account = sys.argv[1] # first command argue is the account name

if account in PASSWORDS:
	pyperclip.copy(PASSWORDS[account])
	print('Password for ' + account + ' copied to clipboard')
else:
	print('There is no account named ' + account)
Now, following instructions I created this batch file:
@py.exe C:\Python36\kodovi\pw.py %*
@pause
...and named it pw.bat

I used the same folder for putting pw.py and batch file pw.bat. Is this ok? The instructions say:
With this batch file created, running the password-safe program on Windows is just a matter of pressing WIN-R and typing pw <account name>.

This doesn't work for me, but I may did something wrong. When WIN-R windows opens I type for example "pw yahoo" but it says that windows cannot find that file.

When I start pw.py the outcome is:
Output:
C:\Python36\kodovi>pw.py yahoo Password for yahoo copied to clipboard
What is actually the correct way to start this program?
Reply
#2
Import should always be first and added f-string.
Also look at argparse which is a step up from sys.argv.
import sys, pyperclip

PASSWORDS = {'yahoo': 'alpha',
             'linkedin': 'beta',
             'youtube': 'beta',
             'job': 'gama',
             'python-io': 'delta' }

if len(sys.argv) < 2:
    print('Usage: python pw.py [account] - copy account password')
    sys.exit()
account = sys.argv[1]
if account in PASSWORDS:
    pyperclip.copy(PASSWORDS[account])
    print(f'Password for {account} copied to clipboard')
else:
    print('There is no account named {account}')
There is no point using bat.
Running should almost always be python my_script.py.
With added command line argument if needed.
Running your code:
E:\div_code
λ which python
/c/python37/python

E:\div_code
λ python pw.py youtube
Password for youtube copied to clipboard
With Batch(bat) people still need to have Python installed.
To make it standalone have to look at eg Pyinstaller.
setup.py also have a option for CLI called entry_points(will make .exe for Windows and executable for Linux),
this is for making it install with pip.
Reply
#3
Cool. I just added
print(pyperclip.paste())
as the last line so program now makes sense. I'm glad to hear that batch files do not have to be created.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  batch file for running python scipt in Windows shell MaartenRo 2 1,899 Jan-21-2022, 02:36 PM
Last Post: MaartenRo
  How we prune Alphabeta Tree Node Using BST concept Anldra12 4 2,429 May-18-2021, 09:17 AM
Last Post: Anldra12
  How to run script from batch file? jmair 1 4,277 Oct-12-2020, 04:29 PM
Last Post: jmair
  python script to Batch File biprabu 1 2,060 Sep-09-2020, 01:49 PM
Last Post: jefsummers
  changing code to work on a batch of images within a folder Afrodizzyjack 2 2,059 May-12-2020, 08:56 PM
Last Post: Afrodizzyjack
  Understanding the concept ( Modules , imports ) erfanakbari1 1 2,198 Nov-25-2019, 01:59 AM
Last Post: Larz60+
  Understand for range concept RavCOder 4 2,796 Oct-29-2019, 02:26 PM
Last Post: newbieAuggie2019
  help with multiprocess concept kiyoshi7 2 2,486 Aug-10-2019, 08:19 PM
Last Post: kiyoshi7
  Batch file not running python script in task scheduler davork 3 4,529 May-09-2019, 12:53 PM
Last Post: Gribouillis
  stiching python and batch jenya56 1 2,531 Apr-02-2019, 06:16 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020