How to enter command line arguments in python notepad++? - python-2.7

I am practicing beginner's Python in Notepad++ and I am stuck when trying to give command line arguments during runtime. When I run the code, I get ValueError: need more than 1 value to unpack, and I can't give input arguments. I tried using Python plugins PyNPP, NppExec and even tried running it through Notepad++'s inbuilt console but I'm still unable to give input. There is no syntactical error in the code:
from sys import argv
script, first, second, third = argv
print "the script is called : ", script
print "your first variable is : ", first
print "your second variable is :", second
print "your third variable is: ", third
Please explain how I can give arguments during runtime. I have searched in Google for possible solutions.

argv is a list of arguments to the python script. It should not be defined inside a script. An example:
python script.py first second third
In this case
sys.argv[0] is script.py
sys.argv[1] is first
sys.argv[2] is second
sys.argv[3] is third

Related

Python 2.7.15 adds a new line in Windows to the end but not on Linux. How do I fix this?

This is not a typical "how do I strip a new line or spaces" question...
I am new to python in general. But I am aware of
print ("test", end="")
print ("test", end="")
for Python 3 and
print "test",
print "test",
for Python 2
Python 3 implementation will print correctly on both Linux and Windows machines; however the Python 2 implementation will add an extra line at the end of the execution on Windows based machines (but not Linux, there it prints correctly). Is there any way to get rid of this new line?
I have searched around and I cant seem to find anyone talking about this particular issue. Here is a screenshot for demonstration:
So, in accordance with the print documentation
Standard output is defined as the file object named stdout
And we probably assume that python I\O are system dependent, so that's how we could try to guess the explanation of this situation, even thought print documentation states:
A '\n' character is written at the end, unless the print statement
ends with a comma.
OR The reason is that Windows & Linux threat print statement differently (since print is a statement in Python 2, and a function call in Python 3).
Back to the question, how to get rid of this line:
I used future statement for print function:
from __future__ import print_function
print('test', end=' ')
print('test', end='')
If I find any reasonable explanation, I will update the answer (should be somewhere !).
After speaking to a few people about this and doing some research. It appears the most straightforward way around this issue is to directly use:
sys.stdout.write()
instead of print. You can then format your output similar to the way C/C++ and Java work.

Python Search String One Liner

I'm trying to use Python a little more and I know this isn't the best use case for it but it's bothering me on why I can't get it to work.
I'm currently using Python 2.7.6 and I want to cat a file and then pull specific strings out of it based on regex. The below code works fine for what I want, but only looks at the first line.
cat /tmp/blah.txt | python -c "import re,sys; m = re.search('Host: (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*OS:(.*) Seq:', sys.stdin.read()); print m.group(1), m.group(2)"
So I assumed I could just use a for loop or fileinput to read the entire file and then put the rest of my code in there but I keep on getting errors with the for loop.
cat /tmp/blah.txt | python -c "import sys; for line in sys.stdin: print line" File "<string>", line 1
import sys; for line in sys.stdin: print line
^
SyntaxError: invalid syntax
I've tried a few variations of this but can't get it to work. It always says invalid syntax at the for portion. I know it has to be something very stupid/obvious I'm doing but any help would be appreciated.
If I created a program called arg.py and put the code below in it and call Python via cat, it works fine. It's just the one liner portion that isn't working.
import sys
for line in sys.stdin:
print line
Unfortunately, constructs that introduce indentation in python like if, for among others are not allowed to be preceded by other statements.
Even in your arg.py file try the following:
import sys; for line in sys.stdin: print line
You will discover that the syntax is also invalid which results to the same error.
So, to answer your question, your problem is not the fact that you ran the program in the terminal but the problem is in python syntax itself. It does not support such syntax
Check out a related question

Why i cant get the process that exist on print?

import os
import time
os.system('WMIC /OUTPUT:C:\Users\PRO\Desktop\ProcessList.txt PROCESS get Caption')
with open('C:\Users\PRO\Desktop\ProcessList.txt', 'r') as f:
if str('System') in f:
print "Yes"
else:
print "No"
I have tried your code, and it was needed some little modification. I did that, but didn't get desirable result. It write successfully in a mentioned file. So, I though it would be better if I print out each line in my python IDLE. I got such weird result.
At first, it looked fine, when open in any text editor. But later, after finding this weird space between each character, I opened this file in Sublime editor, run over there, then found something look like this
After googling a little bit, I found that this is a NUL terminator used to teminate the string in C\C++. Maybe someone with more knowledge about this can explain it better.
I have no idea, how and why it is adding it after every character. But if you remove that and run again the code, you'll get the desirable result.
Kindly find the below updated code:
import os
import time
os.system('WMIC /OUTPUT:C:\Users\sohan.tirpude\Documents\LogInLog.txt PROCESS get Caption')
searchfile = open("C:\Users\sohan.tirpude\Documents\LogInLog.txt", "r")
for line in searchfile:
line = line.replace('\0', '')
#print line
if 'System' in line:
print "Yes"
break
else:
print "No"
Kindly give it a try.

GDB option for 'python - c'

in this document (https://www.exploit-db.com/docs/28553.pdf), what is "r python -c" doing? Thanks.
r is running your program.
Arguments to run are program arguments to pass to your executable.
In this case arguments passed are calculated by running a python expression print "A"*264.
So this runs you program giving it a string consisting of 264 A characters as an argument.

How can I use a letter to select from a multiple choice list?

I'm new to programming as a whole and in my journey to understand it, Python specifically, I am attempting to recreate The Camel Game. The steps I have followed thus far instruct me to create a while loop that will keep looping while a variable done = False. Inside the loop I am instructed to print several choices, selected by a letter, that will eventually do different things. Currently I am attempting to quit the program by selecting option Q, yet I get an error every time I type Q and then enter:
Traceback (most recent call last):
File "/home/pi/Desktop/wasteland_Game.py", line 15, in <module>
user_Choice = input("You decide to: ")
File "<string>", line 1, in <module>
NameError: name 'Q' is not defined
However, if I type "Q" including the quotation marks, the program ends. Onto my question: How do I get only the letter without quotation marks to work as the letter with quotation marks does?
Also this is my first time posting a question here, please excuse any formatting issues.
Thank you in advance,
RavusFlapjack
Python Version 2.7.9
print("Welcome to Wasteland!")
print("You escaped with a Pristine Deathclaw Egg and need to get to Diamond City!")
print("The Deathclaws want their egg back and are chasing you down!")
print("Survive your Wasteland Trek and outrun the Deathclaws!\n")
done = False
while not done:
print("A. Drink from your trusty canteen.")
print("B. Move ahead walking speed.")
print("C. Move ahead full spring.")
print("D. Stop and rest.")
print("E. Check your Pip-Boy for status.")
print("Q. Quit")
user_Choice = input("You decide to: ")
if user_Choice == "Q":
print("Nobody likes a quitter!")
done = True
Try using raw_input() instead of input(). This will still read a line, and the user must press enter.
According to Python 2.7 documentation on input():
Equivalent to eval(raw_input(prompt)).
This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.
If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.
Consider using the raw_input() function for general input from users.
The basic idea is that input() reads a string, then evaluates it (as if it were entered from an interactive shell). It's looking for the symbol (variable, etc.) named Q which doesn't exist, generating an error. You just want the string itself, not the string evaluated.