Pleas help, im not sure whats wrong with the two lines of code - string

I know this is a super super simple thing but dont know why im getting this error. Im just trying to ask for a name and then execute a print command with the input.
name = input("whats your name?:")
print("hello,", name,"!")
whats your name?:Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
Unknown Error.

Use raw_input() inplace of input()
Please find the modifications
name = raw_input("whats your name?:")
print("hello,", name,"!")

Related

Indentation error while importing python file in Juypter Notebook

I am running simple code mentioned below, but getting indentation error. I am using Python 3.x and Juypter notebook. Help will be appreciated.
import os
import sys
sys.path.insert(0, os.path.abspath('C:\dir python util'))
import h
h.my_first_function()
In file h.py, which is in drive c:\dir python util contents are below:
def my_first_function():
print ("my first program of python")
Error I am getting:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 5, in import h
File "C:\dir python util\h.py", line 2 print ("my first program of python") ^ IndentationError: expected an indented block
File "C:\dir python util\h.py", line 2 print ("my first program of python") ^ IndentationError: expected an indented block
Error indicates that it is simple indentation error. You need to maintain the appropriate indentation in the code.
With corrected indentation, it'll be as follows:
def my_first_function():
print ("my first program of python")
So print statement needs to have 4 spaces rather than one.
Suggest you to go through this document to get idea about Python's indentation rules, if you don't know that already.

python3 EOFError: EOF when reading a line

Need help with this, appreciate if someone can try to suggest a fix.
$ echo "print('This works fine')"|python3
This works fine.
But:
$ echo "input('This is NOT working! ')"|python3
This is the output received:
This is NOT working! Traceback (most recent call last):
File "<stdin>", line 1, in <module>
EOFError: EOF when reading a line
It seems you just want to execute python commands inline from bash. For this task you can use this:
python3 -c "input('This also works)"
EDIT:
If you want to use the pipe, you can just take the input from bash and pipe it to python, like this:
#!/bin/bash
read -p "Enter your name : " name
echo "x='$name';print('My name is', x)"|python3

Piping binary data in apache spark spark

So I have an RDD of binary data I create it using
line = sc.binaryFiles("files/Videos",10)
line.map(lambda x:x[1]).pipe("cat").take(1)
I want to pipe this data to an external program but I get the following error
> Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/local/spark/python/lib/pyspark.zip/pyspark/rdd.py", line 723, in pipe_objs
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf3 in position 43: ordinal not in range(128)
Have any Idea how to fix this?
You didn't show us how you passed the data to an external program, but you probably want something along the lines of:
f.write(line.encode('utf8'))
You might prefer to have io.open() create that file handle f for you, using a suitable encoding=, such as 'utf8'.
Do consider moving up from python2 to python3 -- you'll get clearer diagnostics about when there's a missing encode or decode.

How to catch error and stop using IF and ELSE statement?

Hello i am new to python programming. I am self learning by doing a project.
the code below is working fine, but i want to catch the exception if the condition is FALSE. i already have a statement to print if the directory is not there; and continue. I know i can make this code work by nesting the if statement.
but i dont want to do. i want to proceed to next line of code. when i run this as it is, i will get an error if the directory is missing.
import os
goToDir = open("URPsetup.dat","r")
goToDirPath = goToDir.read()
if (os.path.exists(goToDirPath))== True:
os.chdir(goToDirPath)
else: print("directory not found, please check file URPsetup.dat")
goToConfig = open("utility.dat", "r")
print(goToConfig.read())
i get this error message when the directory is not there. What i mean here is that, the directory i supplied in "URPsetup.dat" was incorrect. I did this on purpose to see if the code will stop at the else statement above. it will print the else statement, and continue on to show the error message below. how do i catch this error and stop?
directory not found, please check file URPsetup.dat
Traceback (most recent call last):
File "C:/Users/R82436/PycharmProjects/URP2017/prototype.py", line 9, in <module>
goToConfig = open("utility.dat", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'utility.dat'
directory not found, please check file URPsetup.dat
Process finished with exit code 1
I GUESS you're looking for this:
import sys
.
.
else:
print("directory not found, please check file URPsetup.dat")
sys.exit()
.
.
OR this will do just fine too:
# No need to import sys
.
.
else:
print("directory not found, please check file URPsetup.dat")
raise SystemExit(0)
.
.
That will do the trick ;)
And try to explain your problem properly next time, You aren't completely clear on what is it exactly that you want to do.

Iam not quite sure how to define Users in this case

iam a total noob in python; i have a background in chemistry and iam doing my master in computational chemistry. Iam trying to learn computer science as fast as i can.
I currently dont know how to solve this error. I have googled the question but the answers dont actually satisfy.
I would really appreciate if you guys give me hints on how to fix this error.
Thanks,
Thanh Le
In order for the program to work, it uses codes from this file containing:
from RunRMSD import RunRMSD
RunRMSD()
from SumRMSD import SumRMSD
SumRMSD()
then it uses codes from a file (RunRMSD) containing:
run calcRMSD.py to get raw output from pymol
def RunRMSD():
# get output directory from a threefiles.txt
with open('./threefiles.txt') as fi:
fline = fi.readline()
flist = fline.split('\t')
path_output = flist[1]
import os
command = '/opt/local/bin/pymol -cqr '+'./CalcRMSD.py > '/Users/thanhle/Desktop/output/'RMSD.out'
os.system(command)
Not sure if my path is correct though.
thanhs-MacBook-Pro-2:untitled folder thanhle$ python Director_RMSD.py
Traceback (most recent call last):
File "Director_RMSD.py", line 5, in <module>
RunRMSD()
File "/Users/thanhle/Desktop/ftdock-2-dev2/untitled folder/RunRMSD.py", line 11, in RunRMSD
command = '/opt/local/bin/pymol -cqr '+'./CalcRMSD.py > '/Users/thanhle/Desktop/output/'RMSD.out'
NameError: global name 'Users' is not defined
The "command" variable is not well written:
command = '/opt/local/bin/pymol -cqr '+'./CalcRMSD.py > '/Users/thanhle/Desktop/output/'RMSD.out'
The error is thrown because the path /Users/thanhle/Desktop/output/ is not concatenated and also you are missing a apostrophe. If you don't want to parse any variable to the command it should be written:
command = '/opt/local/bin/pymol -cqr ./CalcRMSD.py > /Users/thanhle/Desktop/output/RMSD.out'

Resources