python 2.7.12 on Linux behaving differently than on Windows
steve+python at pearwood.info
Sun Dec 4 17:52:03 EST 2016
On Mon, 5 Dec 2016 07:26 am, DFS wrote:
> $python program.py column1=2174 and column2='R'
Here is a simple script demonstrating the issue:
# --- program.py ---
print "argv:", sys.argv
print ' '.join(sys.argv[1:])
I haven't tested it on Windows, but on Linux it behaves as you describe (and
as Linux users will expect):
[steve at ando ~]$ python program.py column1=2174 and column2='R'
argv: ['program.py', 'column1=2174', 'and', 'column2=R']
column1=2174 and column2=R
This is *absolutely normal behaviour* for the most common shell on Linux,
bash. I would expect that the other common shells will do the same thing.
Quotation marks need to be escaped if you want the shell to pass them
through to the program, either like this:
or like this:
> It drops the apostrophes, and the subsequent db call throws an error:
> sqlite3.OperationalError: no such column: R
I'm not sure how to interpret this error, so I'm guessing. Please correct me
if I'm wrong, but doesn't this mean that your column is called:
single quote R single quote
that is, literally 'R', which means that if you were using it in Python
code, you would have to write the column name as this?
If so, perhaps the best solution is to get rid of the quotes in the column
names, so that all your users, Windows and Linux, just write this:
program.py column1=2174 and column2=R
(P.S. what happens if they write
program.py column1 = 2174 and column2 = R
> The way it's used in code is:
> argcnt = len(sys.argv)
> querystr = ' '.join(sys.argv[1:argcnt])
You can simplify that to:
querystr = ' '.join(sys.argv[1:])
> I tried using dbl-quotes in the command line, and with the join()
> statement, and neither worked.
By the time you join the arguments, its too late. You have to quote them
> Edit: I got it to work this way:
> but that's bogus, and I don't want users to have to do that.
(1) It's not bogus.
(2) Linux users will expect that you have to escape quotation marks if you
want to pass them through the shell.
(3) If my interpretation is correct, what *is* bogus is that that your
column names include quotes in them. Get rid of the quotes, and your
problem goes away.
If that's not the case, then you'll either have to get your users to escape
the quotes, or you'll have to add them in yourself.
(In case this is not obvious by now, this is not a Python issue. This is
entirely due to the behaviour of the shell.)
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.
More information about the Python-list