So I have a Python program written, and what I am trying to do is to create windows shortcut to the program, and making sure that when running the shortcut, not only the software is ran but also the current directory that the shortcut is at would be passed as sys.argv, so that I could do something to it in the program. I know that for windows shortcut, you can do something like this:
"C:\Documents and Settings\Administrator\Desktop\hello.py" --somevalue
to pass in arguments to the target. I am wondering how could I make it work for my specific case? So for example, if we put the shortcut at Desktop, and run the shortcut, the Python software would start with sys.argv[1]="C:\Users\username\Desktop".
You can specify the current directory for the executed application in a .LNK, that is the normal way to do things if you depend on a specific directory.
If you for some reason actually need to get information about the .LNK you must call the GetStartupInfo Windows function (with ctypes I assume) and if the STARTF_TITLEISLINKNAME bit is set in STARTUPINFO.dwFlags then STARTUPINFO.lpTitle contains the path to the .LNK the user used to start the application.
Related
I'm developing a Python script to do file management on Windows. Essentially, I want to be able to move files to another location, and create in their place a shortcut to a Python script which will do intermediate steps before opening the relocated file. I can currently move files, create functioning shortcuts, and set the shortcut icon image. The only thing I have left is to figure out how to detect the icon which is displayed for the file.
-Need to set the icon for a shortcut (which points to a '.py' script)
-This icon should match that of an arbitrary filetype, exe, etc, which I have the path to
-Hopefully done programmatically through Python. os.system() calls are acceptable. Extra credit is given for not requiring additional modules (though I already have win32com, winshell, pythoncom, etc)
Thanks in advance...
File type information is found in the registry. You could look up the file extension's type with the windows registry module.
Edit: It appears icons are available through shell32, as seen here. The Python equivalent seems to be ctypes.windll.shell32.ExtractIconA, though I'm not sure about the syntax.
I want to know how (if there is a way) to write a function and have it automatically defined when you start a python shell, or to keep certain defined functions after the shell closes so you don't have to keep re defining it if you use it a lot.
Yes, you can, you need to set your environment variable PYTHONSTARTUP to a python script you would like to be executed at every python startup.
By convention, such files are named by adding the rc suffix to the program name you're trying to tweak, and are usually located at the root of your home directory. In that case I would create the python file under $HOME/.pythonrc and then run export PYTHONSTARTUP=~/.pythonrc (for UNIX-like systems, it could be slightly different on Windows if you don't have a MinGW or equivalent).
Here's an example of .pythonrc file that you can play with: https://gist.github.com/twneale/5245670
I am making an application with python which will need to be associated with a file type. I need a cross platform solution (can be different for different platforms) that includes setting the icon for the file type, having my application open when a file of that type is double-clicked and being able to reference that file when my application is opened.
To clarify
Lets say that I have an application called FooEdit, that edits .foo files. .foo files have the mime type of text/plain. Here is the process that happens when a user first gets FooEdit:
A user obtains the executable for my application, whether it be on Linux, Windows, or OS X.
A python script runs that:
Sets FooEdit (the executable) as the default application for .foo file extensions.
Sets the icon for .foo files to foofileicon.png (or foofileicon.ico if that works better).
The same user double clicks a .foo file.
FooEdit opens with the path to the file in it's argv array (I'm using PyQt4)
I have seen this question, but it only refers to Mac OS X and the user must do it manually, I want the solution to work on all three of the major platforms (Linux, Windows, OS X) and I want it to be done automatically by the python script.
I have also heard about inserting values into the windows registry for such a purpose and I am fine with doing that for the windows version of my program. However that still leaves me clueless as to how to do the same on Mac and Linux.
I would like a single solution that works on all platforms, though I accept that it might well not be possible.
How does one acquire the directory of the Windows Shortcut calling a python script. I'd like to have multiple shortcuts in various places pointing to one script that I could edit if I wanted, but where the directory from which the script is called is accessible. Is this possible?
This script would likely then be compiled with py2exe or something, so if it is something that isn't possibly until THAT stage, I could go with that. Thanks!
The easiest solution is to just use a batch script instead of a shortcut.
All it needs is python C:\path\to\script\script.py, and the script will have the correct CWD.
The default when creating a shortcut on windows is for the Start in: property to be set to the folder the linked file resides in.
The script has no knowledge of the fact that it was called from a shortcut, let alone where that shortcut resides.
You can change the Start in: property of the shortcut to the path of the folder the shortcut resides in.
Then you can use os.getcwd() to get that path.
Unfortunately setting Start in: to . doesn't work.
In general you cannot obtain this information. You should use argv to switch behaviour of your script.
you can do so with
import os
print os.getcwd()
os.getcwd()
Return a string representing the current working directory.
Availability: Unix, Windows.
Yes, I've searched. So after spending about 4-5 hours struggling just to get Python files running, I recently stumbled over the solution to get it running through the environment variables like this: cmd -> python -> Python starts, yay yay
Since it didn't work to do it through the command line and similar I had to do it manually through the Windows interface. Now that it's working, however I cannot open .py files without typing out the full path like this: python C:\X\X\X\test.py which is obviously also starting to get annoying.
So now I'm trying to find out which variable I have to change (yet again) to only be able to type 'python test.py' and have it running. Sorry if I come off vague, but it's always a major pain to setup a new programming language for me and it kills my mood.
Thanks for help, it'll be really appreciated.
When you say
able to type 'python test.py'
I'm not sure exactly what you mean. Normally when the Python interpreter runs, it looks in the current directory for any source file that is named on the command line (unless you specifically name a location for the source file, as you've discovered). It seems from your previous statement:
python C:\X\X\X\test.py which is obviously also starting to get annoying
that your test.py file exists somewhere else.
What you might want to try is to change the current directory first, before running your script. In a command prompt window, type:
C:
cd \X\X\X
python test.py
(obviously substituting your actual path name). My apologies if you already know this.
To make python executable on your command line, you need to add it to your PATH environment variable, which it sounds like you have done on the command line. It is quite simple to add directories to the PATH in Windows if you know where to look. Essentially, you need to get to the Environment Variables dialog box, which is slightly different for each version of Windows.
For Windows XP: Start -> Control Panel -> System -> Advanced -> Environment Variables
For Windows Vista, 7: Click the Start Orb, right-click Computer and select Properties -> Advanced -> Environment Variables
Then, in the lower of the two boxes, find Path and click Edit. Change it so that C:\Python27 (or whichever version of Python you have) is at one end of the list, separated from the other entries by a semicolon (e.g. C:\Python27;C:\Program Files ...)
Once you've done this, python will work at the command line whenever you open a command window.
Regarding your second issue, however, there isn't much you can do. You must either specify the complete path to your script or already be in the same directory as the script. That is, if the script is in C:\X\X\X you will either need to invoke it as C:\X\X\X\test.py or first cd C:\X\X\X.