Trying to use Python for more than text parsing and figure the best way is to write a script I will find useful. I'm having an issue though I get an error when making the call.
from subprocess import call
call (["powercfg", "/batteryreport"])
The error I receive is the following:
An unexpected error condition has occurred. Unable to perform operation. You may not have permission to perform this operation.
I've run the command prompt under elevated user privileges as well with no dice. Any help would be appreciated.
Note: For those unfamiliar this command creates an HTML file of your battery use on Windows in your user directory (C:\Users\Name).
Perhaps you can use the full path to powercfg.exe?
Perhaps you can call([ ... ], shell=True)?
Perhaps you can check the return value of call()?
Related
When I try to use the script from afl-unicorn I get the error below when I try to run it:
AttributeError("'NoneType' object has no attribute 'GetTriple'")
Related line is arch, arch_vendor, arch_os = lldb.target.GetTriple().split('-') but it works when I just type lldb.target.GetTriple() on interactive console. I don't know why it does not work when importing as comments in the script states.
I tried to add the script as a command later by doing command script add dumper -f unicorn_dumper_lldb but this throws error: unable to execute script function; so that didn't work.
Any idea why?
The error message is because lldb.target is None. To understand why this is so, see the couple of paragraphs in this section of the lldb Python reference after the table:
https://lldb.llvm.org/use/python-reference.html#embedded-python-interpreter
The lldb.target, etc variables are only defined when running in the embedded script interpreter. It doesn't make sense to use a global "selected target" in general purpose scripting, since lldb supports more than one target at a time, and you have no way of knowing that the currently selected target will be the one you want to operate on.
To work properly, the script will have to figure out how to pass the target it intends to work on around within the script.
I'm currently doing some work in a server (Ubuntu) without admin rights nor contact with the administrator. When using the help(command) in the python command line I get an error.
Here's an example:
>>> help(someCommand)
/bin/sh: most: command not found
So, this error indicates that most pager is not currently installed. However, the server I'm working on has "more" and "less" pagers installed. So, how can I change the default pager configuration for this python utility?
This one is annoyingly difficult to research, but I think I found it.
The built-in help generates its messages using the standard library pydoc module (the module is also intended to be usable as a standalone script). In that documentation, we find:
When printing output to the console, pydoc attempts to paginate the output for easier reading. If the PAGER environment variable is set, pydoc will use its value as a pagination program.
So, presumably, that's been set to most on your system. Assuming it won't break anything else on your system, just unset or change it. (It still pages without a value set - even on Windows. I assume it has a built-in fallback.)
You can make a custom most script that just invokes less (or even more).
The steps would be:
Set up a script called most, the contents of which are:
#!/bin/sh
less ${#:1} # wierdess is just "all arguments except argument 0"
Put that script in a location that is on your PATH
Then most filename should just run less on that file, and that command should get called from in your python interpreter.
To be honest though, I'd just use Karl's approach.
You can view the various pager options in the source code. That function can be replaced to return whatever is desired. For example:
import pydoc
pydoc.getpager = lambda: lambda text: pydoc.pipepager(text, 'less')
For me, the following fails
open("~/somefile")
but
open(os.path.expanduser("~/somefile"))
works.
Why does it work this way and how can I fix it?
In Linux, the expansion of ~ into your home directory is performed by the shell before your program even receives the command line parameter. Python does not use the shell to open a file, it's done at a lower level. This is not a bug.
os.path.expanduser exists precisely to provide this capacity when you need it.
open is a rather simple implementation and doesn't translate ~ to $HOME. That translation needs to be done before passing the path in. There's also pathlib.Path.expanduser.
I'm trying to incorporate the program TOPCAT (which has really amazing plotting capabilities) into a python script I have written. The problem is that when I make a call to the program it tells me:
OSError: [Errno 2] No such file or directory
Here's some background to the problem:
1) The way I usually open up topcat through the command line is through the alias I have created:
alias topcat='java -jar /home/username/topcat/topcat-full.jar'
2) If I'd like to open TOPCAT with a file in mind (let's use a csv file since that's what I'd like it to work with), I would type this into the command line:
topcat -f csv /home/username/path_to_csv_file/file.csv
And that also works just fine. The problem comes about when I try to call these commands while in my python script. I've tried both subprocess.call and os.system, and they don't seem to know of the existence of the topcat alias for some reason. Even doing a simple call like:
import subprocess
subprocess.call(['topcat'])
doesn't work... However, I can get topcat to open if I run this:
import subprocess
subprocess.call(['java','-jar','/home/username/topcat/topcat-full.jar'])
The problem with this is that it simply opens the program, and doesn't allow for me to tell it which file to take in and what type it happens to be.
Could somebody tell me what I'm doing incorrectly here? I've also looked into the shell=True option and it doesn't seem to be doing any better.
Okay - so I'm really excited that figured it out. What worked before was:
import subprocess
subprocess.call(['java','-jar','/home/username/topcat/topcat-full.jar'])
It turns out it can take more command line arguments. This is what eventually got it open up with the correct comma separated file through the command line:
import subprocess
subprocess.call(['java','-jar','/home/username/topcat/topcat-full.jar','-f','csv','/full/path/to/data.csv'])
Hopefully this is enough information to help other people who come across this specific task.
If anyone else comes across this, pystilts might be of interest.
https://github.com/njcuk9999/pystilts
edit: It is a native Python wrapper of Topcat/STILTS.
I am trying to figure out the best solution to accomplish this. Basically, I want to open another program from Python (doesn't matter, could be an image, executable, etc). I have tried os.system and subprocess.call however both will not terminate the script after, and will instead wait for a return. I have looked at os.execl, and it seems to close to what I need, but I am not sure if I understand the arg's as I always get exec format errors and invalid arguments. I am not even sure if this is the proper function for what I need. Any help would be appreciated.
I have tried using subprocess.call and subprocess.Popen using something similar to this:
import subprocess
subprocess.Popen("B:\test.txt")
and it ends up with the following error:
WindowsError: [Error 5] Access is denied.
Use subprocess.Popen[docs]
Just don't call communicate on the resulting object.
os.execlp("start", r"B:\test.txt")
(That's for Windows. On a Unix system running X11, you'd do this:)
os.execlp("xdg-open", r"B:\test.txt")