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

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

Related

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

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'

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)

How to work around no output redirection in Windows powershell [duplicate]

This question already has answers here:
How to redirect the output of a PowerShell to a file during its execution
(10 answers)
Redirecting standard input\output in Windows PowerShell
(5 answers)
Closed 9 years ago.
I have written a lot of code that relies on output redirection in cygwin.
I now have to integrate some libraries that aren't compatible with cygwin and that need in my case Windows or Linux.
Is there a smooth way to go from cygwin's python script.py 42 <in.txt >out.txt to powershell's equivalent? I know powershell can't use input/output redirection, at least not with the <, > (the documentation says it can weirdly) but is there another way?
PowerShell definitely can do input/output redirection, it just doesn't do it the exact same way as bash.
I'm assuming you don't want to actually learn PowerShell, just get a quick & dirty answer. So, here goes.
For simple cases, bash-like syntax works fine:
foo > out.txt
When that doesn't work, often just wrapping up the tool in Powershell scriptlet is all you need. In other words, create a foo.ps1 file with this in it:
foo > out.txt
Then just run foo.ps1 instead of foo.
You may also want to look at the Tee-Object, Set-Content, Add-Content, and Start-Transcript, etc. cmdlets. Start with A Task-Based Guide to Windows PowerShell Cmdlets.
For example, Start-Transcript captures standard output as a "dumb string" and writes it to a file. So, one equivalent of foo > out.txt is:
Start-Transcript -path out.txt
foo
Stop-Transcript
Or, for foo >> out.txt:
Start-Transcript -path out.txt --append
foo
Stop-Transcript
Similarly, in the other direction, Get-Content reads a text file and prints it out as a dumb string, which you can pipe to your program:
Get-Content in.txt | foo
Of course another obvious possibility is to change your Python script to take filenames.
In many cases, just using fileinput.input() instead of sys.stdin gives you this for free—you can pass it a file's content via stdin, or a filename on the command line (or all kinds of fancier combinations) and the script will see it the same way.

SyntaxError: Invalid Syntax in python when trying to use tarfile.open() on a .tgz file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Syntax error on print with Python 3
I want to view the contents of a .tgz file and I found python's tarfile module. I found the following tutorial which looked promising. http://www.doughellmann.com/PyMOTW/tarfile/
Here is my python file below:
import tarfile
tar = tarfile.open("exampleTar.tgz","r")
print tar.getnames()
When I actually execute my python file, I get a carrot sign pointing at the 'r' in the last line and the error message: SyntaxError: invalid syntax.
Print is function in python 3.x.
import tarfile
tar = tarfile.open("exampleTar.tgz","r")
print(tar.getnames())

Categories