Example cx_oracle does not work, python3.3 - python-3.x

When I execute my file test_cx_oracle.py with a python 3.3 interpreter it errors with the following output.
**File "test_cx_oracle.py", line 3
**print con.version"**
^
SyntaxError: invalid syntax**
The contents of this file are as follows,
import cx_Oracle
con = cx_Oracle.connect('system/diamondmine#127.0.0.1/XE')
print con.version
con.close()
What does this error mean?

In python 3.x the the print statement has been replaced by a print function.
Since functions must be called by having a set of trailing () (which contain arguments to the function), you must add them to print calls in python 3.x. In the case of the print function the usual syntax is just to pass the value you wish to be printed to the print function directly.
With that in mind, changing line 3 to the following should correct your error.
print(con.version)

Related

python3: how can I paste code directly into console that calls a function instead of having to load a .py file?

In a python3 console I just want to copy and paste code directly into the console without loading a .py file but I get an error, probably because it's executing only one line at a time?
>>> def k():
... print("Hi")
... k()
File "<stdin>", line 3
k()
^
SyntaxError: invalid syntax
>>>
How can I run multi-line python code by just copying and pasting what I've written, into console instead of loading a .py file? I realize people will say it's stupid to do this, but hypothetically if it weren't stupid to do this, what's the easiest way to do it?
your problem is this:
you have to paste the def k(): after the prompt >>> then paste your function code one line at a time after ... (with indents) then at the end of your function definition add an empty line (no indent) then you will get the >>> prompt again where you paste your function call k() .
this was what I got
>>> def k():
... print("hi")
...
>>> k()
hi
>>>

importing script in python terminal

Trying to import a python script file into terminal (invalid syntax)
Hi, I am trying to import a python script file in my python terminal however a is giving me an error when the script name starts with a number or includes certain characters like _ (I am still a beginner)
This works fine:
>>> import a00
Bright Green
However this give me invalid syntax:
>>> import 00a
File "<stdin>", line 1
import 00a
^
SyntaxError: invalid syntax
or this
>>> import 00_a
File "<stdin>", line 1
import 00_a
^
SyntaxError: invalid token
Python module (meaning a .py file) names follow the same naming rules as variables, so they can only start with underscore or a letter. After the first character, you can use numbers also.
(Dashes are also technically allowed, but should be avoided since using them requires special syntax.)
The preferable convention for module names is to have them be all lowercase characters, and underscores if needed.
You can read more from the Python PEP 8 Style Guide.
Yes, names cannot begin with digits, so any name like 0abcd or 1nfi, but not a00, will be invalid.

Am observing issues printing in python when using os.listdir

I
am not able to understand why all below prints work when I comment the first one. But when I uncomment the first print(os.listdir....), I get an error from the interpreter: why is this so?
import os
print(os.listdir(r"\\ftlengnas.eng.test.net\CWCDevops$\Dms\Staging\APS\Roles\\")
print('c:\waste')
print(r'c:\waste')
print(r'c:\waste\\')
print("c:\waste\\")
OutPut:
print('c:\waste')
^
SyntaxError: invalid syntax
Process finished with exit code 1
I don't get any error when i comment the first print(os.listdir.....)
You are missing the last ) in that print statement.
print(os.listdir(r"\\ftlengnas.eng.test.net\CWCDevops$\Dms\Staging\APS\Roles\\")

Python Invalid syntax - Invalid syntax [duplicate]

This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 5 years ago.
I have the following piece of code to pull the names of all files within a particular folder (including all files in it's subfolders):
import sys,os
root = "C:\Users\myName\Box Sync\Projects\Project_Name"
path = os.path.join(root, "Project_Name")
for path, subdirs, files in os.walk(root):
for name in files:
print os.path.join(path, name)
Unfortunately, it throws the following error:
> File "<ipython-input-7-2fff411deea4>", line 8
> print os.path.join(path, name)
> ^ SyntaxError: invalid syntax
I'm trying to execute the script in Jupyter Notebook. I also tried saving it as a .py file and running it through Anaconda prompt but received the same error. Can someone please point out where I'm going wrong? I'm pretty new to Python.
Thanks
in python3, print function needs to be like this :
print(os.path.join(path, name))
For more information on the changes within print function from python 2 to 3, check these links :
print is a function
pep-3105
What is the advantage of the new print function in Python 3.x over the Python 2 print statement?
This is a Python 2 Vs Python 3 issue.
In Python 2, print is used without parenthesis like:
print 42
In Python 3, print is a function and has to be called with parenthesis like:
print(42)

How to call from within Python an application with double quotes around an argument using subprocess?

I'm trying to call certutil from inside python. However I need to use quotation marks and have been unable to do so. My code is as follows:
import subprocess
output= subprocess.Popen(("certutil.exe", "-view", '-restrict "NotAfter <= now+30:00, NotAfter >= now+00:00"' ), stdout=subprocess.PIPE).stdout
for line in output:
print(line)
output.close()
I thought the single quotes would allow me to use double quotes inside the string.
Also I have tried using double quotes with the escape character (\"), however I keep getting the same error:
Unknown arg: -restrict \\NotAfter\r\n'
For some reason it seems to be translating " into \\.
Can anyone give insight as to why and how to fix it?
I do not have Python in any version installed. But according to answer on How do I used 2 quotes in os.system? PYTHON and documentation of subprocess, subprocess handles requirement for double quotes on arguments with space automatically.
So you should need to use simply:
import subprocess
output= subprocess.Popen(("certutil.exe", "-view", "-restrict", "NotAfter <= now+30:00, NotAfter >= now+00:00" ), stdout=subprocess.PIPE).stdout
for line in output:
print(line)
output.close()
And the command line used on calling certutil is:
certutil.exe -view -restrict "NotAfter <= now+30:00, NotAfter >= now+00:00"
output=subprocess.Popen(("certutil.exe -view -restrict \"NotAfter<=now+30:00,NotAfter>=now+00:00\"" ),stdout=subprocess.PIPE).stdout
This is what was needed. I was passing the command as a command with 2 args. What I should have been doing was passing it as one big command with 2 parameters.

Resources