Python invalid syntax in print - python

Sorry I am not familar with Python...
It gives me the following error message
File "gen_compile_files_list.py", line 36
print 'java files:', n_src
^
SyntaxError: invalid syntax
I.e. caret points to last quote. What's wrong with it?
OS Windows 7, Python version 3.2.2

On Python 3, print is a function. You need this:
print('java files:', n_src)

print changed syntax between Python2 and Python3; it is now a function.
You would need to change:
print 'java files:', n_src
to
print('java files:', n_src)
Alternatively, you can try converting the code from Python2 to Python3 syntax with the 2to3 tool. Here is more information on the transition if you are interested. This way you can maintain a single code base that works on both versions.
As you are not familiar with python, try installing Python 2 instead and running the code with that.

print is a function in Python 3+. So:
print ('java files:', n_src)

Related

How can I debug this Python syntax error with f string

def write_users_group(heading_writer, heading, user_writer, users):
heading_writer.writerow([f"{heading}", f"{len(users)} users"])
user_writer.writeheader()
for user in users:
user_writer.writerow(user)
heading_writer.writerow([" "])
Error:
File "/Users/ashirwadniv/Downloads/gitlab/users.py", line 59
heading_writer.writerow([f"{heading}", f"{len(users)} users"])
^
SyntaxError: invalid syntax
This syntax is only allowed for Python version 3.6 and above. Check/Upgrade your Python version.
Fyi, there is nothing wrong with your syntax provided the python version supports it.

Translating .unpack from Python 2.7 to 3.9

I am working on rewriting a piece of code that was written for python 2.7. I am having trouble with the line below. It doesn't give any errors, but the self.gear value doesn't look right.
self.gear = struct.unpack("I", self.mm[64:68])[0]
struct.unpack("I",some_bytes[0:4]) works exactly the same in py2 and py3

Issue with printing without creating newline in Python when running from terminal

I am just starting up with Python and was trying to figure out how to print the contents of a list of items without getting a newline for each new item printed. I've looked around on the web for solutions, one of which being replacing print(x) with print(x, end="") when printing an item x. My simple code is as follows:
list = [1, 2, 3, 4, 5]
for x in list:
print(x, end="")
I'm of course expecting the simple output of
12345
which I get when i run the script in PyCharm, but when I try to run the code from the terminal I get the error message:
print(x, end="")
^
SyntaxError: invalid syntax
Why is this happening? I am running Python 3.6 by the way.
This syntax is available only in Python3. You need to check your Python version again because it has to work as you see below:
>>> python test.py
File "test.py", line 4
print(x, end="")
^
SyntaxError: invalid syntax
>>> python3 test.py
12345
a=[1,2,3,4,5]
print(*a,sep='')
output
12345

What is wrong with this python command?

Let me first say that I'm a newbie to python, and I've written a python shell named test.py. The content is as follows:
#!/usr/bin/python
import os
cur_dir = os.getcwd();
print 'hello world' > cur_dir+"/test.log" 2>&1
When I run test.py by "python test.py", it says:
File "test.py", line 4
print 'hello world' > cur_dir+"/test.log" 2>&1
^
SyntaxError: invalid syntax
Could anyone give me some idea? Thanks!
P.S.
what I want is to write a string in test.log file. That's all. As I said first, I'm REALLY new to python, so please don't be so harsh to me :), I'm gonna read through the tutorial first to have a glimpse of python, Thanks to all you guys!
You are using invalid syntax. As a result Python gives you the error:
SyntaxError: invalid syntax
To fix this, stop using the invalid syntax.
Also in the future, explain what you are trying to achieve, and your answers are likely to tell you how to achieve that.
The invalid syntax looks like it's a bash redirection of stderr to stdout which makes no sense in the context above, so it's not possible to figure out what you are actually trying to do.
To write to a file you do this:
thefile = open('path_to_the_file', 'wt')
thefile.write('The file text.\n')
However, to do logging, you are better off using the logging module.
Use '>>' with print to redirect it to an alternate stream.
See How to print to stderr in Python?

Import glob, invalid syntax python

Okay, I'm a noob when it comes to Python, I have to learn this for work.. And so far, I'm looking at some small programs to list directories.
I'm using Python 3.2.1.. In the Python Shell, I make a new window and I put:
import glob
print glob.glob("/*.txt")
But when I "run module", I save it, and it always tells me Invalid syntax, and it highlights the 2nd glob in the code.. Why?? Any idea on how to fix this? I don't really understand why I have an error..
print is a function in Python 3. You can't use it as a statement like you would in the 2.x versions. Your code should work if written as:
import glob
print(glob.glob("/*.txt")) #Note the parens for print()

Categories

Resources