I have an invalid syntax according to flake8, pylint, but the code works.
What's wrong with that code?
I did lots of Google search but couldn't find anything.
#!/usr/bin/env python
with open("test.py", "a") as output:
# E: 4, 0: invalid syntax (<string>, line 4) (syntax-error)
print("hello world", file=output)
What version of Python are you running? I'm not sure when it was implemented exactly but I don't think earlier versions of Python had a file=output parameter for the print() function so your interpreter might only be expecting a string
Related
I want to make a log function in my script but I don`t know why it gives me this error
File "c:/Users/x/x/x", line 33
log = open(f"Log/{realDate.strftime("%x")}.txt", "a")
^
SyntaxError: invalid syntax
Here`s the code that is causing the problem
realDate = datetime.datetime.now()
log = open(f"Log/{realDate.strftime("%x")}.txt", "a")
log.write("Hello Stackoverflow!")
log.close()
Can somebody please help me?
The problem is that you are trying to nest double quotes inside double quotes; f-strings don't allow that. You would need to change one or the other to another style of quotes. For example:
log = open(f'Log/{realDate.strftime("%x")}.txt', "a")
The main problem is that you aren't using a version of Python that supports f-strings. Make sure you are using Python 3.6 or later. (Update: Even in Python 3.6 or later, the identified location of the error can shift:
>>> f"{print("%x")}"
File "<stdin>", line 1
f"{print("%x")}"
^
SyntaxError: invalid syntax
>>> f"{print("x")}"
File "<stdin>", line 1
f"{print("x")}"
^
SyntaxError: invalid syntax
)
I am trying to run a function called read_distribution.py in a Python package called RSeQC. However when I run the following command:
python3 read_distribution.py -i mysample.bam -r hg38_RefSeq.bed
I get the following error:
File "distribution.py", line 278
print "%-30s%d" % ("Total Reads",totalReads)
^
SyntaxError: invalid syntax
Lines 275-282 in the read_distribution.py code look like this:
except StopIteration:
print >>sys.stderr, "Finished\n"
print "%-30s%d" % ("Total Reads",totalReads)
print "%-30s%d" % ("Total Tags",totalFrags)
print "%-30s%d" % ("Total Assigned Tags",totalFrags-unAssignFrags)
print "====================================================================="
Is this a problem with my python version? I do not know enough Python to figure out the problem so any help is appreciated-Thanks!
I bet you're using Python 3.X. Starting with 3.0, the print statement became a function, requiring parentheses to be used like when calling any function. So the code you show needs to look like this to work in Python 3.X:
print("%-30s%d" % ("Total Reads",33))
print("%-30s%d" % ("Total Tags",33))
print("%-30s%d" % ("Total Assigned Tags",12))
print("=====================================================================")
There are scripts on the internet that will convert much of your Python 2.X code to 3.X if you have a bunch more of it to convert. Alternately, if you got the code from somewhere else, maybe they have a Python 3.X version available.
I believe this package was written in Python 2, which didn't have you putting ()'s after print, in Python 3 this changed to have you put ()'s after print, You're using Python 3.
I run a script with the code to open a file and it returns SyntaxError. The script is an open source script that I want to test.
with open(f"/home/mine/myfoldr/myapp.yml", "r") as file:
The line above returns the following error:
File "./startup.py", line 28
with open(f"/home/mine/myfoldr/myapp.yml", 'r') as file:
^
I just don't understand what does it mean with f" here, after open(f"...). Because normally it will be write something like below, without f.
with open("/home/mine/myfoldr/myapp.yml", "r") as file:
I think its not a typo because other line code in the script also..have the same style f, for example:
print(f"Which section do you want to change?"
f"[Application/Controller/Database]")
The f at the start of strings is called f-string, introduced with PEP 489 starting at Python 3.6.
It is used for string formatting, similar to .format(). There are a lot of tutorials on it you can read. The basic example:
x = 22
print('this is {}'.format(x))
print(f'this is {x}')
Here, both lines will output the same resulting string this is 22.
You probably get the error because you are using a version older than Python 3.6, some version where f-strings are not supported.
To test the third-party code you will have to use a newer Python version or modify the code yourself (but this last options may be a lot of work and may introduce some unintentional errors).
In my Visual Code Studio running python3.6 - my code is saved as "Langemead12Test.py" w/ lines as:
!C:\Users\Bones\Anaconda3\python.exe
[1]def readFastq(SRR835775_1.first1000.fastq)
Red Error underline def [pylint] E0001:invalid syntax (, line 3).
In Anaconda command prompt running python3.6:
File "Langmead12Test.py", line 3
def readFastq(SRR835775_1.first1000.fastq)
^
SyntaxError: invalid syntax
Question(s): I'm a total newb here but I can't seem to understand why VCS throws an error under def and my anaconda command line prompt throws error at the . within the fastq filename. Are these independent errors or different representations of the same error? All thoughts and hints are welcome.
Background: I am attempting to execute exercise in ADS1: Practical: Working with sequencing reads. Ben Langmead's Youtube class on reading fastq files (filename = SRR835775_1.first1000.fastq).
You have 2 syntax error in one line:
1.Missing "" or this '' before and after your string
2.You forget to put ":" and the end of the line:
Correct syntax:
def readFastq(filename="SRR835775_1.first1000.fastq"):
Read this carefully please:
https://docs.python.org/3/tutorial/introduction.html#strings
https://docs.python.org/3/tutorial/controlflow.html#defining-functions
And after that read and learn everything from there:
https://docs.python.org/3/tutorial/index.html
I have the following python script which i want to run..
However, it keeps showing the error message on my command prompt whenever i attempt to run the script.
Error message:
File "xor.py", line 9
File = open(sys.argv[1], 'rb').read<>
SyntaxError: Invalid Syntax
The following is the command i executed in cmd:
python xor.py sample_output.txt 'what would the secret be?'
The following is the script:
# xor.py
import sys
from itertools import cycle
file = open(sys.argv[1], 'rb').read()
string = sys.argv[2]
sys.stdout.write(''.join(chr(ord(x)^ord(y)) for (x,y) in zip(file, cycle(string))))
You are not running the code you are editing, instead you are running a different file than the one you edited.
This is because there is no syntax error in the code that you have provided. However, there is a syntax error in the code in the error message:
File = open(sys.argv[1], 'rb').read<>
This ends with <>, not with (). I assumed this to be a transcription error, but you say that the error message really appears like this, although the code does not.
Hence: You are running a different file than the one you are editing.
You have .read<> when you probably intended .read()
First of all, "file" is already reserved; that is built-in keyword so unable to set as the name of variable.
And second, do not use <> instead of (). incorrect in grammar.
The problems might be clearly solved if you code like:
fd = open(sys.argv[1], 'rb').read()