I have a script for sending emails using SMTP_SSL. The code is working fine in PyCharm but in the terminal I get an error.
This is the code:
import smtplib
s = smtplib.SMTP_SSL("smtp.googlemail.com:465")
mml=input("enter your email address :\n")
str(mml)
passr=input("enter your pass:\n")
str(passr)
s.login(mml,passr)
em = input("please type the email you want to send :\n")
str(em)
a = input("please type the message:\n")
str(a)
s.sendmail(mml,em,a)
print("\nEmail Was Sent..:)")
When I run this in my terminal its giving this after i enter the email:
enter your email address :
mahmoud.wizzo#gmail.com
Traceback (most recent call last):
File "medo.py", line 3, in <module>
mml=input("enter your email address :\n")
File "<string>", line 1
mahmoud.wizzo#gmail.com
^
SyntaxError: invalid syntax
When I am trying to put the email between quotes, e.g. "mahmoud.wizzo#gmail.com" its working fine.
How can I run my script in the terminal?
I suspect you're running your script using Python 2 on the command line. The behaviour of input() changed in Python 3.
Try running python3 my_file.py instead of python my_file.py.
This is actually the way Python works. The problem you have is that input takes input from the user and then "executes" or "compiles" it so if you enter 4+9 in the input it will produce the number 13. Not the string "4+9". So try using raw_input() in Python 2. You can also use sys.stdin.readline() to get a string I'm any Python version. Don't forget import sys.
Related
Learning python as a beginner. I wanted to copy my code into CMD but it won't work. Here is code
calculation_to_units = 24
name_of_unit = "hours"
def days_to_units(number_of_days):
if number_of_days > 0:
return f"{number_of_days} days are {number_of_days * calculation_to_units} {name_of_unit}"
else:
return "Liczba dni musi być dodatnia :)"
user_input = input("Hello user, enter amount of days you want to calculate to hours\n")
user_input_number = int(user_input)
calculated_value = days_to_units(user_input_number)
print(calculated_value)
despite the fact that it works in Pycharm. I already checked paths. I am not able to solve this problem. When I type in python3 test.py it also says C:\Users\Borys\AppData\Local\Programs\Python\Python310\python.exe: can't open file 'C:\Users\Borys\test.py': [Errno 2] No such file or directory
Also recieved this message "unable to initialize device prn in python"
My internet connection is so bad that it took me 10 minutes to sign up on stack overflow. Additionaly my english knowledge is too small for complex programming explainations.
It can be difficult to paste code that calls input() into a python shell. Both the shell and the input() function read stdin. As soon as python reads the line with input(), it calls input() and that consumes the next line on stdin. In your case, that was a python code line intended to set a variable. That line was consumbed by input and was not read or executed by python. So you got a "variable not defined" error. But you would also have gotten another error because that line was also not the stuff you wanted to input.
Suppose you had the script
val = input("Input value: ")
i_val = int(val)
print(i_val)
And pasted it into the python shell
>>> val = input("Input value: ")
Input value: i_val = int(val)
>>> print(i_val)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'i_val' is not defined. Did you mean: 'eval'?
The line i_val = int(val) was assigned to val - it was not interpreted by the shell. There would be an ">>> " if it did.
I am learning to program in Python and am still at the very beginning.
I wrote a 2 scripts to cut out IP-addresses from a nmap-output.
The first script cuts out the IP-addresses:
import re
file = open("test.txt", "r")
ips = open("ips.txt", "w+")
for line in file:
ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}', line)
if "filtered" in line:
ips.write(str(ip) + "\n")
This code works fine on Windows and Linux, but (I hope I'm right) the for-loop gets every line as a list. That means, my IP-addresses have the format ['x.x.x.x'].
I wrote a second script to delete all the unnecessary characters ([ ' and ]):
ip = open("ips.txt", "r")
ip_filtered = open("ip_filtered.txt", "w")
for line in ip:
s = line
neu = s.translate(({ord(i): None for i in '[\']'}))
ip_filtered.write(str(neu))
This script works well on Windows (I got a new file, just with IP-addresses), but on Linux I get the following error:
Traceback (most recent call last):
File "zeichen_loeschen.py", line 6, in <module>
neu = s.translate(({ord(i): None for i in '[\']'}))
TypeError: expected a string or other character buffer object
What's the reason for this error?
Thanks in advance :)
I get the same message when running with the python command on linux (which uses python 2.7). Try running it with the python3 command and your code should work.
When running python in netbeans, when I type in input, how and where do I take in input. eg
name = input("enter your name")
print("hello",name)
When I run this I get the output on the left in the shell but when I try and input anything in that window I get
Traceback (most recent call last):
File "C:\Users\Rahul\Documents\NetBeansProjects\NewPythonProject\src\newpythonproject.py", line 18, in <module>
name = input("enter your name")
File "<string>", line 1, in <module>
NameError: name 'hi' is not defined
I am using netbeans 8.2.
thanks!
When you use input() in Python 2, it performs and eval() on the user input.
To get the exact string the user typed, you have to use raw_input() to get exactly what the user typed.
Also in Python 2.7, the print is a statement rather than a function. You can lose the parentheses.
your code will look like,
name = raw_input("enter your name")
print "hello", name
If You use python version 2+
name = raw_input("enter your name")
print("hello {}".format(name))
If You use python version 3+
name = input("enter your name")
print("hello {}".format(name))
I am using PyCharm CE 2016.2 and python interpreter 3.5. When I tried to type the following in console, the file is generated with '123'.
>>> with open('man_data.txt', 'w') as data:
print('123', file=data)
However, when I put the same statement into a test.py file and try to run it using subprocess.call(['python', 'test.py']), it gives
>>> subprocess.call(['python', 'test.py'])
File "test.py", line 2
print('123', file=data)
^
SyntaxError: invalid syntax
1
Does anyone has any clues? Million thanks!
Python 2 is started when you call subprocess.call(['python', 'test.py']).
Try to change to subprocess.call(['python3', 'test.py']) or update the script as described here.
I have Spyder 2.3.8 that I installed with Anaconda.
Python version is 2.7.11, conda version is 4.0.5.
I have found that some types of code make the Spyder editor stop responding. One example is the line:
x = b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A'
I assume this is a valid python code. I am relatively new to Python, so please correct me if I am wrong here. I tried in on a terminal on the Python shell and it works.
Another very strange example is
png = 'oxy.png'
f = open(png, 'rb')
f.read(10) # That executes without any problems
f.close()
f = open(png, 'rb')
x = f.read(10) # this line makes Spyder freeze!
f.close()
The only difference here is that I assign f.read() to a variable, and that makes Spyder stop responding!
I just updated Spyder and all other anaconda packages. The error was happening before and it still happens. Any idea of what I could be doing wrong, or what could I try?
EDIT: There was this other part of the question which has been explained already (thanks!)
I also tried to run the following example I found on the internet:
name = input("What is your name? ")
print("Nice to meet you " + name + "!")
This example gives me an exception. Here is the full output:
name = input("What's your name? ")
print("Nice to meet you " + name + "!")
What is your name? sininho
Traceback (most recent call last):
File "<ipython-input-1-e82cc0e3f7a3>", line 1, in <module>
name = input("What's your name? ")
File "/scr/elbe9/pauline/physio/program/python_libraries/anaconda2/lib/python2.7/site-packages/ipykernel/ipkernel.py", line 165, in <lambda>
builtin_mod.input = lambda prompt='': eval(self.raw_input(prompt))
File "<string>", line 1, in <module>
NameError: name 'sininho' is not defined
In Python 2, the input() function executes the code you type in. To get q string, use raw_input() function which works the same way.