Is there a way to obtain the original argv value from python after execution code like this:
sys.argv[0]=""
In fact I search for the unwritable version of sys.argv.
I would prefer a OS independent solution, but I'm working on windows, so a windows only solution would be sufficient
On Linux you could use:
open("/proc/{}/cmdline".format(os.getpid())).read()[:-1].split("\x00")[1:]
Related
I am writing a script in Python 2.7.
It needs to be able to go whoever the current users profile in Windows.
This is the variable and function I currently have:
import os
desired_paths = os.path.expanduser('HOME'\"My Documents")
I do have doubts that this expanduser will work though. I tried looking for Windows Env Variables to in Python to hopefully find a list and know what to convert it to. Either such tool doesn't exist or I am just not using the right search terms since I am still pretty new and learning.
You can access environment variables via the os.environ mapping:
import os
print(os.environ['USERPROFILE'])
This will work in Windows. For another OS, you'd need the appropriate environment variable.
Also, the way to concatenate strings in Python is with + signs, so this:
os.path.expanduser('HOME'\"My Documents")
^^^^^^^^^^^^^^^^^^^^^
should probably be something else. But to concatenate paths you should be more careful, and probably want to use something like:
os.sep.join(<your path parts>)
# or
os.path.join(<your path parts>)
(There is a slight distinction between the two)
If you want the My Documents directory of the current user, you might try something like:
docs = os.path.join(os.environ['USERPROFILE'], "My Documents")
Alternatively, using expanduser:
docs = os.path.expanduser(os.sep.join(["~","My Documents"]))
Lastly, to see what environment variables are set, you can do something like:
print(os.environ.keys())
(In reference to finding a list of what environment vars are set)
Going by os.path.expanduser , using a ~ would seem more reliable than using 'HOME'.
On Windows if i use MKS toolkit shell, os.getcwd() function returns value in lower case. However on using windows cmd, it returned exact path.
Is it possible in Python by any means for os.getcwd() to return the exact path (without converting to lower case on Windows)?
Are you sure about this behavior? It's not documented, seems counter-intuitive, and I'm not able to reproduce it (on Windows 7 using Python 2.7.2):
>>> import os
>>> print os.getcwd()
C:\Users\foofoofoo
Note the capital characters at the start.
Before starting Python and using os.getcwd(), in your console you probably used "cd c:\your_path". It matters if this 'c' is lower or upper.
I would like to know if there is a way I could check from my python code if matlab exists on a system. So far the only thing I can come up with is: exists = os.system("matlab") and then parse the exists for a command not found. But I'm almost sure this will:
Start matlab in case it exists on the system. I don't want this.
The response may vary depending on the system I'm running ?
So is there any way I could check if a matlab installation is available on the system from python ?
Regards,
Bogdan
Assuming your system call works, you can check the path for matlab.exe like this:
import os
def matlab_installed():
for path in os.environ["PATH"].split(";"):
if os.path.isfile(os.path.join(path, "matlab.exe")):
return True
return False
For Unix, you have to change split(";") to split(":") and "matlab.exe" to whatever the matlab executable is called under Unix.
Another way would be to to use shutil
import shutil
mt = shutil.which("matlab")
If 'matlab' is found it returns the path where it was found, else it returns 'NoneType'. You may check for 'matlab' or 'matlab.exe' depending on the OS.
Ok guys I imagine this is easy but I can't seem to find how to copy a string. Simply COPY to the system like CTRL+C on a text.
Basically I want to copy a string so I can for example, lets say, paste(ctrl+v).
Sorry for such a trivial question, haha.
For Windows, you use win32clipboard. You will need pywin32.
For GTK (at least on GNU/Linux), you can use pygtk.
EDIT: Since you mentioned (a bit late) you're using wxPython, they actually have a module for this too, wx.Clipboard.
This depends a lot on the OS. On Linux, due to X's bizarre selection model, the easiest way is to use popen('xsel -pi'), and write the text to that pipe.
For example: (I think)
def select_xsel(text):
import subprocess
xsel_proc = subprocess.Popen(['xsel', '-pi'], stdin=subprocess.PIPE)
xsel_proc.communicate(some_text)
As pointed out in the comments, on a Mac, you can use the /usr/bin/pbcopy command, like this:
xsel_proc = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
If you want to support different OSes, you could combine different solutions with os.name to determine which method to use:
import os, subprocess
def select_text(text):
if os.name == "posix":
# try Mac first
try:
xsel_proc = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
except:
# try Linux version
xsel_proc = subprocess.Popen(['xsel', '-pi'], stdin=subprocess.PIPE)
elif os.name == "nt":
# Windows...
For Windows, you can do this and it's much easier than creating a new subprocess etc...
For a multi-platform solution you will need to use a cross-platform framework like wxPython or PyQt - they both have support for reading and writing to the system clipboard in a platform independent way.
I'm trying to use python to run a program.
from subprocess import Popen
sa_proc = Popen(['C:\\sa\\sa.exe','--?'])
Running this small snippit gives the error:
WindowsError: [Error 2] The system cannot find the file specified
The program exists and I have copy and pasted directly from explorer the absolute path to the exe. I have tried other things and have found that if I put the EXE in the source folder with the python script and use './sa.exe' then it works. The only thing I can think of is that I'm running the python script (and python) from a separate partition (F:).
Any ideas?
Thanks
As the docs say, "On Windows: the Popen class uses CreateProcess() to execute the child program, which operates on strings. If args is a sequence, it will be converted to a string using the list2cmdline() method.". Maybe that method is messing things up, so why not try the simpler approach of:
sa_proc = Popen('C:\\sa\\sa.exe --?')
If this still fails, then: what's os.environ['COMSPEC'] just before you try this? What happens if you add , shell=True to Popen's arguments?
Edit: turns out apparently to be a case of simple mis-spellling, as 'sa' was actually the program spelled SpamAssassin -- double s twice -- and what the OP was writing was spamassasin -- one double s but a single one the second time.
You may not have permission to execute C:\sa\sa.exe. Have you tried running the program manually?