[Python] sys.argv command line arguments passed in

Source: Internet
Author: User

The book on a topic need to implement the function "according to the account password, copy password to the Clipboard." The account number and password are stored in the code dictionary.

Problem Description:

As a python novice from the code (see below) only know:

1. SYS.ARGV initially contains a row of file paths.

2. The account to be queried needs to be transferred to SYS.ARGV.

How does it work?

#! Python3#pw.py-an inssecure Password locker program.#Run This program in CMD, the parameter (account) load after filename. Like:pw.py EmailPasswords={'Email':'F7minlbdduvjjux','Blog':'VmALvWyKAxiVH5','Luggage':'12345'}ImportSys,pyperclipifLensys.argv)<2:              Print('Usage:python pw.py [Account]-Copy account password') Sys.exit () account=Sys.argv[1] #First command line ARG was the account nameifAccountinchPASSWORDS:pyperclip.copy (Passwords[account])Print('Password for'+account+'copied to clipboard.')Else:              Print('There is no account named'+Account )

Solution Ideas:

command-line arguments, which are manipulated by the command line.

In the CMD CD to pw.py directory, the parameter is placed after the file name when the command is hit. Like, it's done.

d:\div\pyscripts>Blog for blog copied to clipboard. D:\DIV\PyScripts>luggage for luggage copied to clipboard. D:\DIV\PyScripts>email for e -mail copied to clipboard. D:\DIV\PyScripts>

Note:

SYS.ARGV imports with import, the initial value is the path of the running file + filename, the account value after the incoming account is added to the SYS.ARGV, as follows:

['d:\\div\\pyscripts\\pw.py'email']

When the program finishes running, it tries to print sys.argv, and the passed parameters are freed.

The above is a novice python for sys.argv a little superficial understanding, if there is no understanding of the place, welcome to correct.

[Python] sys.argv command line arguments passed in

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.