22

I would like to write a python script that would upload any file I select in Windows Explorer. The idea is to select any file in Windows Explorer, right-click to display file's Context Menu and select a command from there... something like "Upload to Web Server".

After the command is selected, the Python runs a script which receives the file-path and a file name of the file to be uploaded. The writing the Python script that will upload the file to web seems to be straightforward. What is unclear is how to create an entity in Windows Context Menu for the Python Script. And how to pass the file path and file name to the Python script to catch.... Please advise!

dreftymac
  • 31,404
  • 26
  • 119
  • 182
alphanumeric
  • 17,967
  • 64
  • 244
  • 392

7 Answers7

34

Assuming Windows 7, If you open a folder and type "shell:sendto" in the address bar then hit enter you'll be taken to the context menu. You can add a .cmd file with the following in it.

@echo off
cls
python C:\Your\File\uploadscript.py %1

This should execute your python script passing in the file (%1) as a parameter. Within the python script you can use:

import sys
sys.argv  #sys.argv[1] is the file to upload

This gets all parameters passed in so sys.argv[1] should get you the file that was passed in. I tested this and it works. The reason you need the .cmd file instead of going right to the .py is because the .py file wont show up in the Send To menu.

More information on getting the file passed in is here:
Accepting File Argument in Python (from Send To context menu)

EDIT: Adding script for calling on multiple files. Note this calls the python script on each individual file, if you want to send all the files as a parameter to the python script then you'll need to do a bit more work. You need to research batch scripting if you want to do more advanced things.

@echo off
cls
:upload_loop
IF "%1"=="" GOTO completed
  python C:\Your\File\uploadscript.py %1
  SHIFT
  GOTO upload_loop
:completed
Community
  • 1
  • 1
Tyler Ferraro
  • 3,753
  • 1
  • 21
  • 28
  • 2
    How would you pass multiple files at one? I can only get one to be passed through. – Tristan Forward Dec 10 '13 at 23:09
  • Adding a method above that will work. If you want to do it in a single call to python then you'll have to use variables, this might be of use: http://en.wikibooks.org/wiki/Windows_Batch_Scripting – Tyler Ferraro Dec 21 '13 at 08:26
  • 3
    @TristanForward Probably just use `%*` instead of `%1`. – binki Mar 28 '17 at 19:01
11

Instead of %1 use %*.

%1 will pass in the first argument, %* will pass all (%n will pass in the nth...)

@echo off
cls
python C:\Your\File\uploadscript.py %*

Note that the command line has built in character limits 2047 for XP and prior, 8191 for windows 7 and later

xav
  • 5,452
  • 7
  • 48
  • 57
Brian
  • 313
  • 2
  • 7
5

To add things such as python scripts to right click context menu, also possible is to add register keys (regedit) in

\HKEY_CLASSES_ROOT\Directory\Background\shell

There, add a container, name it the string you want to appear in the context menu. In it, add a key of type REG_SZ, which contains the python script launcher for instance

C:\Python27\python.exe "C:\path\to\your\script\yourscript.py"

I do not know how to make that work with the aforementionned solution for getting multiple file selections into sys.argv, but I thought this would me worth mentioning here as well.

user3367775
  • 51
  • 1
  • 1
2

This webpage Adding Windows context-menu actions has a nice python script that will register a context menu to pass the file path to your python script. I have not tried but it looks easy to modify this sample to what you need to do. Plus, this way it is one click less than sendTo solution I guess.

otterb
  • 2,660
  • 2
  • 29
  • 48
1

As a current beginner programmer, was none of these answers helpful. I asked a friend and he helped me with it.

Here is my solution:

Create in regedit a shortcut:

  1. Press Windows Button+ R, typ in the field "regedit"
  2. Choose where the files need to be placed (I would suggest "HKEY_CURRENT_USER\AllFilesystemObjects\shell & HKEY_CURRENT_USER\AllFilesystemObjects\shellex ". Like that your shortcut shows on files, folder and programm)
  3. Create a new key in that folder
  4. Create a new key inside your new folder and call i "command"
  5. In "command" you now put your file path that needs to be executed. On the end of it you put a "%1"

It should look like this:"C:\Python\folder\file.py" "%1" (Yes it works with the python file but you can also use a cmd file)

(We had a error so he changed the it a bit on my current commend file it looks like that: C:\Users\UserName\AppData\Local\Programs\Python\Python38\python.exe "C:\Python\folder\file.py" "%1"

The reason I cant tell you, but it works :D )

For the python file, I used the following:

import sys
sys.argv[1]
print("It worked",sys.argv[1])
while True:
v=1

With that, you have everything you need to have a shortcut and import the path.

Mister MK
  • 11
  • 2
0

In addition to previous answers, it is also possible to use a python script in the SendTo folder.

This allows:

  • usage of try... except blocks around import statements, in case your library has moved or whatnots;
  • usage of for loops on the list of arguments.

Example :

import sys

try:
    import foo
except ImportError as exc:
    print("cannot start!", exc)
    input("press Enter to close")
    sys.exit()

try:
    foo.main(sys.argv[1:])
except Exception as exc:
    print(f"Unexpected error! {exc}")
    input("press Enter to close")
    sys.exit()

input("press Enter to close")

I recommend that this script is kept minimal, and provides just the launch of a well-built Python package, with version control and classical development methods.

Joël
  • 2,723
  • 18
  • 36
0
#step 1
#create python file name   test.py 
#this is the contenet of test.py
import sys
print(sys.argv)
input('quit ! ')

#step 2 
#convert test.py   to test.exe

#step 3
"""
chose any file and click open with and chose your (test.exe) 
now look the list printed you will understand ! 

    """
mohammed
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Ajeet Verma Apr 24 '23 at 09:09
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34257975) – Nikolaj Š. Apr 26 '23 at 13:30