THIS TURNED OUT TO BE A SYNTAX ERROR ON MY PART A LINE EARLIER IN THE CODE.
Hello, I'm having some trouble with a nested function I wrote in python. Here is the relevant code.
device = "/dev/sr0"
def burn():
global device
burnaudiotrack(device)
createiso(device)
burntrack2(device)
I'm confused, because every time I try to run the script, python returns this:
File "./install.py", line 72
burnaudiotrack(device)
^
SyntaxError: invalid syntax
I've nested functions before, and done so in a similar manner. I feel like I'm missing something fairly obvious here, but I can't pinpoint it. Thank you for your help/suggestions!.
EDIT:
Full code: (I tried to just post relevant info in the original)
http://dpaste.com/hold/291347/
It's a tad messy, and there may be other errors, but this one is vexing me at the moment.
You are missing a close parenthesis on line 61.
Looks like the quote and paren at the end of the line are swapped.
speed = raw_input("Recomended(4);Default(8))"
should be
speed = raw_input("Recomended(4);Default(8)")
The code you have pasted into your question appears to have tabs as well as spaces. You should (according to PEP-8) always use spaces for indenting in Python. Check your text editor settings.
What's probably happened is you have some mix of tabs and spaces that looks correct in your editor, but is being interpreted differently by the Python compiler. The Python compiler sees a different inconsistent indenting, and throws a SyntaxError.
Update: As another answer points out, you are missing a closing parenthesis on a line of code you didn't show in your original question. Nevertheless, my comments about tabs in your source still hold.
Related
I have tabs and indents|indent set to 2:
However the actual indentation is coming up as 3 - specifically inside the groupby function - which I have unindented and re-indented several times to be sure the behavior were consistent[-ly incorrect]:
Note that I have also tried the Auto-indent (Option-Command-I) - it also indents stuff to 3 spaces instead of 2. Bit strange..
Any thoughts?
The first thing you might want to check is to make sure you don't have Detect and use existing file indents for editing enabled in Settings/Preferences | Editor | Code Style. If you do, when you auto-indent, it might apply the wrong settings.
You can try to fix incorrectly formatted code by using Option+Command+L (Mac) / Control+Alt+L (PC), which is the Reformat Code option. That should fix the code and then auto-indent should work correctly.
This is most annoying problem in pycharm. Here is simple answer to this :
Just follow the path :
GO to Edit->Convert Indents->To Spaces
This will actually insert space instead of tabular character in empty space when you default enter for next line.
Am I correct in thinking that that Python doesn't have a direct equivalent for Perl's __END__?
print "Perl...\n";
__END__
End of code. I can put anything I want here.
One thought that occurred to me was to use a triple-quoted string. Is there a better way to achieve this in Python?
print "Python..."
"""
End of code. I can put anything I want here.
"""
The __END__ block in perl dates from a time when programmers had to work with data from the outside world and liked to keep examples of it in the program itself.
Hard to imagine I know.
It was useful for example if you had a moving target like a hardware log file with mutating messages due to firmware updates where you wanted to compare old and new versions of the line or keep notes not strictly related to the programs operations ("Code seems slow on day x of month every month") or as mentioned above a reference set of data to run the program against. Telcos are an example of an industry where this was a frequent requirement.
Lastly Python's cult like restrictiveness seems to have a real and tiresome effect on the mindset of its advocates, if your only response to a question is "Why would you want to that when you could do X?" when X is not as useful please keep quiet++.
The triple-quote form you suggested will still create a python string, whereas Perl's parser simply ignores anything after __END__. You can't write:
"""
I can put anything in here...
Anything!
"""
import os
os.system("rm -rf /")
Comments are more suitable in my opinion.
#__END__
#Whatever I write here will be ignored
#Woohoo !
What you're asking for does not exist.
Proof: http://www.mail-archive.com/python-list#python.org/msg156396.html
A simple solution is to escape any " as \" and do a normal multi line string -- see official docs: http://docs.python.org/tutorial/introduction.html#strings
( Also, atexit doesn't work: http://www.mail-archive.com/python-list#python.org/msg156364.html )
Hm, what about sys.exit(0) ? (assuming you do import sys above it, of course)
As to why it would useful, sometimes I sit down to do a substantial rewrite of something and want to mark my "good up to this point" place.
By using sys.exit(0) in a temporary manner, I know nothing below that point will get executed, therefore if there's a problem (e.g., server error) I know it had to be above that point.
I like it slightly better than commenting out the rest of the file, just because there are more chances to make a mistake and uncomment something (stray key press at beginning of line), and also because it seems better to insert 1 line (which will later be removed), than to modify X-many lines which will then have to be un-modified later.
But yeah, this is splitting hairs; commenting works great too... assuming your editor supports easily commenting out a region, of course; if not, sys.exit(0) all the way!
I use __END__ all the time for multiples of the reasons given. I've been doing it for so long now that I put it (usually preceded by an exit('0');), along with BEGIN {} / END{} routines, in by force-of-habit. It is a shame that Python doesn't have an equivalent, but I just comment-out the lines at the bottom: extraneous, but that's about what you get with one way to rule them all languages.
Python does not have a direct equivalent to this.
Why do you want it? It doesn't sound like a really great thing to have when there are more consistent ways like putting the text at the end as comments (that's how we include arbitrary text in Python source files. Triple quoted strings are for making multi-line strings, not for non-code-related text.)
Your editor should be able to make using many lines of comments easy for you.
When I tried to run this code, it gives me the error message of line21 UnexpectedIndent. How do I go about this? Thanks.
for filepath in matches:
with open (filepath,'rt') as mytext:
mytext=mytext.read()
print re.findall(r'NSF\s+Org\s+\:\s+(\w+)',mytext) #This line just aims to diagnose the problem.
matchOrg=re.findall(r'NSF\s+Org\s+\:\s+(\w+)',mytext)[0]
capturedorgs.append(matchOrg)
When I got rid of the print re.findall(r'NSF\s+Org\s+\:\s+(\w+)',mytext), the error message is MatchOrg... list out of range.
Going to meeting. Will check back all replied after 4p.
The line mytext=... is indented with a tab, and the rest is indented with spaces. Use either tabs or spaces, but don't mix them. The use of spaces is encouraged over the use of tabs.
In your text editor use the option to show all characters. Make sure indentations are consistently represented either with spaces or tabs and not a mix of both.
At times when you copy snippets of code from other sources, the indentation from your source and the other may differ causing the python interpreter to throw the unexpected indentation error as below
I am writing a python script using version 2.7.3. In the script a line is
toolsDir = 'tools/'
When I run this in terminal I get SyntaxError: invalid syntax on the last character in the string 'r'. I've tried renaming the string, using " as opposed to '. If I actually go into python via bash and declare the string in one line and print it I get no error.
I checked the encoding via file -i update.py and I get text/x-python; charset=us-ascii
I have used TextWrangler, nano and LeafPad as the text editors.
I have a feeling it may be something with the encoding of one of the editors. I have had this script run before without any errors.
Any advice would be greatly appreciated.
The string is 'tools/'. toolsDir is a variable. You're free to use different terminology, of course, but you'll end up confusing people trying to help you. The only r in that line is the last character of the variable name, so I assume that's the location of the error.
Most likely you've managed to introduce a fixed-width space (character code 0xA0) instead of an ordinary space. Try deleting SP=SP (all three characters) and retyping them.
Try running the code through pylint.
You probably have a syntax error on a nearby line before this one. Try commenting this line out and see if the error moves.
You might have a whitespace error, don't forget whitespace counts in python. If you've mixed tabs and spaces anywhere in your file it can throw the syntax checker off by several lines.
If you copied and pasted lines into this from any other source you may have copied whitespace in that doesn't fit with whichever convention you used.
The error was, of course, a silly one.
In one of my imports I use try: without closing or catching the error condition. pylint did not catch this and the error message did not indicate this.
If someone in the future has this triple check all opening code for syntax errors.
I'm a Python beginner and now it's freakin me out:
L = []
file = urllib.urlopen("http://someurl.com/someText.txt")
line = file.readline()
while line != "" :
L.append(line)
line = file.readline()
appuifw.selection_list(choices=L)
and I get this error:
line = file.readline()
^
SyntaxError: invalid syntax
Does anyone know what the issue is?
Rewriting to
file = urllib.urlopen("http://blabla.com/bla.txt")
lines1 = file.readlines()
for li in lines1:
L.append(li)
index = appuifw.selection_list(choices=L)
it seems to work now.
(Still problems left but I think it's the URL)
Show the invisibles. I bet there is an illegal character (null is a favorite) hiding in one of those lines and it's not showing up on your screen. Or maybe the file has the wrong type of line ends.
My usual tricks here:
1) You might have typed it in right in StackOverflow; try copying this code back into the source and see if it fixes things. Sometimes it's hard to see if you put a ] where a ) or } should be.
2) Comment out all the lines, then uncomment them one at a time until the syntax error reappears. If the syntax error is there when you comment out all the other lines, then ther real problem is upstream.
3) Delete the line in question and a couple lines below and above it. Delete these lines in a single operation; you don't want the bad character to stay around because it was in between two lines that you deleted one at a time. Then retype those lines. Don't paste them back in; that might just paste the problem right back in.
You are overwriting the built-in function file with you variable of the same name. Maybe that causes the Py60 some grief?
I actually don't see a problem, unless you're mixing tabs and spaces in that indentation, in which case the error should complaining about indentation levels. But I thought I'd point out that there's a much cleaner way to read all the lines in a file-like object:
f = urllib.urlopen("http://someurl.com/someText.txt")
lines = f.readlines()
appuifw.selection_list(choices=lines)
Seems to work fine in my Python interpreter (2.6.1).
I take it you did do import urllib first? (Not doing that would cause a NameError, not a SyntaxError.)
EDIT: a quick Google found this: http://discussion.forum.nokia.com/forum/showthread.php?t=150563
It’s 18 months old, but it claims that PyS60 is Python 2.2.2. I don’t have that on my machine, but it might be worth seeing if that’s the issue.