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.
Related
Trying to save image files in batches. Works nicely, but the list of names for each file sometimes includes apostrophes, and everything stops.
The offending script is:
pic.save(r"C:\Python34\Scripts\{!s}.jpg".format(name))
The apostrophes in the names aren't a problem when I embed them in a url with selenium
browser.get("https://website.com/{!s}".format(name))
or when I print the destination file name, e.g.
print(r"C:\Python34\Scripts\{!s}.jpg".format(name))
Which is fine to turn out like
C:\Python34\Scripts['It's fine'].jpg
so I assume this kind of problem has something to do with the save function.
The trace back calls the pic.save line of code in PIL\Image.py and says the OSError: [Errno 22] is an Invalid argument in the save destination.
Using Windows 7 if that matters.
Probably super-novice error, but I've been reading threads and can't figure this out--workaround would be cleaning the list of apostrophes before using it, which would be annoying but acceptable.
Any help appreciated.
---edited to fix double quotes as single, just mistyped when writing this post...doh.
It's not a Python problem, but Windows, or rather the file system, file naming rules. From the MSDN:
Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:
The following reserved characters
< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
On UNIX type systems, all except the / would be valid (although most would be a bad idea). A further "character", binary zero 0x00, is invalid on most file systems.
Rules for URLs are different again.
So you are going to have to write a sanitiser for filenames avoiding these characters. A regular expression would probably be the easiest, but you will have to choose replacement characters that don't occur naturally.
Edit: I was assuming that Error 22 was reporting an invalid filename, but I was wrong, it actually means "The device does not recognise the command".
See https://stackoverflow.com/questions/19870570/pil-giving-oserror-errno-22-when-opening-gif. The accepted reply is rather weird though.
I Google'd "python PIL OSError Errno 22", you might like to try the same and see if any of the conditions apply to you, but clearly you are not alone, if that's any consolation.
Sorry I can't do more.
I'm trying to create a simple string like:
test = "abc#email.com" in Pydev but it automatically interpret "#" as a special symbol and the statement cannot pe done.
When I focus on the variable in Pydev, I can see:
test = "abc*#email.com"* instead of test = "abc#email.com"
Anyone has any idea why I have this issue?
If I run the statement in windows command prompt python, then it is correctly assigned.
Does it have any relation with pylint?
Thanks,
The only way I was able to reproduce similar result was with unclosed string before that, which is obviously a syntax error. Otherwise I've never seen any problems with string literals containing # while using PyDev (with PyLint).
try %#?
this might be your answer. Not a whole lot out there about special characters in python.
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'm trying to open a program while I'm in a python script using the subprocess.call() function, It opens the program but for some reason the program doesn't allows that and just throw an "Unhandaled exception" error, I know the problem is probably in the program so there may be any other command that will open a program, fill some fields and press "Submit?"
Thanks!
Edit: I've no code to post..
str = 'd:\Softwares\X.exe'
subprocess.call(str)
I've also tried with:
subprocess.call(str,shell=True)
Try calling another program the same way. If the problem persists, the problem is with your code. If it goes away, the problem is with the program.
I think changing to 'D:/Softwares/X.exe' or one of the other string formats will help because the '\' character is the escape character ... used for example to denote a new line '\n'.
It probably works if you use forward-slashes (backslashes are escape symbols in Python). If it doesn't, write the first line like this:
str = r'd:\Softwares\X.exe'
The r tells Python that you are creating a raw string, so it will ignore escape symbols. More information at: https://docs.python.org/2/reference/lexical_analysis.html#string-literals
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.