zsh error when executing python code as a script [duplicate] - python

This question already has an answer here:
Passing a url as argument
(1 answer)
Closed 2 years ago.
I have a Python script which uses sys to accept arguments -
import sys
url = sys.argv[1]
I need to provide it with a bunch of urls to parse. The script works perfectly on a jupytor notebook (after hard-coding the arguments in the code: url = 'http://www.hello.com') notebook but when I try to execute it as a script I get errors like these, for various URLs -
for 'http://www.blog.example.com:123/path/to/file.html?key1=value1'
[1] 85926
zsh: no matches found: http://www.blog.example.com:123/path/to/file.html?key1=value1
[1] + exit 1 python -m urlparser
for 'https://www.hello.com/photo.php?id=2064343443411&set=a.2634433167446&type=3&hall'
zsh: parse error near `&'
Meanwhile, the script works fine for simpler URLs like https://blog.hello.com/one/two
What could be the issue? Encoding problems?

I figured -
Have to put the arguments in quotes like - 'http://www.blog.example.com:123/path/to/file.html?key1=value1'

Related

os.system not writing in terminal [duplicate]

This question already has answers here:
Python: How to get stdout after running os.system? [duplicate]
(6 answers)
Closed 6 years ago.
i have a simple test file i created in order to use vmd (a program for my job)
This test file is as simple as :
import os
os.system("vmd -eofexit < VMD_script.tcl -args 3spi_cholesterol")
Basically, im using os.system to launch a program name vmd with another script i wrote and im giving it one argument. What i found it is that when i run this test script, i get nothing done but if i just go in terminal and write :
vmd -eofexit < VMD_script.tcl -args 3spi_cholesterol
everything works perfectly. Is there anything im doing wrong with os.system? I have been using this line for a while now but on linux and it was working perfectly, could it be a mac issue?
Thanks allot
import subprocess
ls_output = subprocess.check_output(['vmd', '-eofexit', '<', 'VMD_script.tcl', '-args', '3spi_cholesterol'])

How to subprocess this call in python: png2pos args > /dev/usb/lp0 [duplicate]

This question already has answers here:
How to redirect output with subprocess in Python?
(6 answers)
Closed 7 years ago.
I currently have the following code:
subprocess.call(["png2pos", "-c", "example_2.png", ">", "/dev/usb/lp0"])
The program png2pos is being accessed because it's giving me the message:
This utility produces binary sequence printer commands. Output have to
be redirected
This is the same error I get if I forget to type in > /dev/usb/lp0, so I'm fairly certain it has something to do with the '>' character. How would one redirect this output to /dev/usb/lp0 with subprocess?
To make sure the output is redirected properly, you need to set shell to True and pass a single string:
subprocess.call("png2pos -c example_2.png > /dev/usb/lp0", shell=True)
Otherwise ">" is treated as a program argument.
I do not have your tool installed, so I cannot really test here. But had an issue with redirecting output from a console application using python before. I had to redirect it using the command itself, not via the shell (as you are trying)
with open("/dev/usb/lp0", 'wb') as output_str:
subprocess.Popen(["png2pos", "-c", "example_2.png"], stdout=output_str)

Error when calling `python -m pydoc read()` [duplicate]

This question already has answers here:
Can't find any info on Python's read() method (python 2.7)
(2 answers)
Closed 7 years ago.
I'm learning python from LPTH.
In exercise 15 in study drills, I'm supposed to know what read() does using pydoc; However when I try to do so with, python -m pydoc read(), I get an error like this.
an expression is expected after << ( >>
at line : 1 character 23
python -m pydoc read ( <<<< )
*category info : parser error : (:), parentcontainsErrorRecordException
* FullyQualifiedErrorID: ExpectedExpression
I don't understand what I did wrong.
I used the same way for: raw_input,os,open, but apparently I am doing something wrong with read().
You probably want this:
python -m pydoc read
But that will give you: no Python documentation found for 'read'
In what context are you trying to use read()... Example: to open a file and read and see associated docs, use
python -m pydoc file

calling bash configuration files in python script to setup environmental variables [duplicate]

This question already has answers here:
Emulating Bash 'source' in Python
(6 answers)
Closed 7 years ago.
I have a text file contains all the paths belong to bunch of commands that can be called in the bash scrpits and it is called progs.ini.
Usually when I want to call this configuration file in my bash script I use this command
. progs.ini
progs.ini contains stuff for instance like this:
BIN=/bin/linux_64/
P_ANALYSE=${BIN}/analyse
NPARA=1
now I want to use some part of my code in python and I was trying to use this command as following:
import subprocess as S
import os
CMD='. progs.ini'
S.call([CMD],shell=True)
It doesn't return any error message but it can not recognise the variables which are defined in progs.ini
>>os.system('echo ${BIN}')
0
Well it is not about setting some environmental variable which is similar to this problem. I want to set some variables using the configuration file.
You seem to be using Linux. In that case I would be inclined to put a cat /proc/$$/environ at the end of your ini file. That will print out all the key value pairs in a format that's easy to parse. This should do:
s = os.popen(". whatever.ini && cat /proc/$$/environ").read()
env_vars = {x[:x.find("=")]:x[x.find("=")+1:] for x in s.split("\00")[:-1]}
Tested. That didn't work but this did:
s = os.popen(". ./whatever.ini && set").read()
env_vars = {x[:x.find("=")]:x[x.find("=")+2:-1] for x in s.split("\n")[:-1]}
print env_vars['hello']

How can I get the full response after pinging a server and assign it to a variable in Python? [duplicate]

This question already has answers here:
Running shell command and capturing the output
(21 answers)
Closed 8 years ago.
I want to be able to ping a server, and instead of just being returned with 0 or 1, I want to be able to see what you would see if you ran it in terminal your self, for example be able to see how many milliseconds it took for the packet to reach the host and get back.
I am using Python 2.7 and am on Linux, which is why in my code I use -c and not -n. This is the code I am using:
import os
response = os.system("ping -c 1 8.8.8.8)
print response
You should use subprocess.check_output:
https://docs.python.org/2/library/subprocess.html#subprocess.check_output

Categories