How to select many files and pass to a Python script

Quick to answer questions about finding your way around Linux Mint as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums in the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
starplanet

How to select many files and pass to a Python script

Post by starplanet »

I have a Pyhton script.
I need to be able to select many files in explorer (I assume it's called explorer?)...
And then call the script and pass the full file names to the script.
Full = give full path.

So when the code runs, this is what get's run:

myscript.py "/home/My User/My Sub Folder/abc.txt" "/home/My User/My Sub Folder/pqr.txt" "/home/My User/My Sub Folder/xyz.txt" "/home/My User/My Sub Folder/123.txt"

I put quotes around the names as there will be spaces in folder or file names.

How can I do this?

In Windows, i have a DOS script that I put in the SendTo folde. Then I can select files, right click and choose the file from the SendTo drop down.
It's more of a hack than anything. Ideally I would want to have be part of the Windows right click menu in explorer.

Thanks.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29614
Joined: Wed Jul 06, 2011 3:58 am

Re: How to select many files and pass to a Python script

Post by xenopeek »

How to do this likely depends on which file manager (or "explorer" using the name Windows gives to it) you're using. Which edition of Linux Mint do you have? Cinnamon, MATE or Xfce? If you're unsure see if you have System Info in your menu where you should be able to find it. Else open the terminal from your menu and run the command inxi -S to find it.
Image
User avatar
AndyMH
Level 21
Level 21
Posts: 13748
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: How to select many files and pass to a Python script

Post by AndyMH »

You could probably do all this in python (and it might be simpler) or write a bash script that does something along the lines of:

cd to your folder
ls to get all files > some_variable
parse some_variable to put the list into one or more variables in the format required
execute your python script with myscript.py $variable_containing_filenames.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
xenopeek
Level 25
Level 25
Posts: 29614
Joined: Wed Jul 06, 2011 3:58 am

Re: How to select many files and pass to a Python script

Post by xenopeek »

AndyMH wrote: Sun Jan 13, 2019 6:32 am ls to get all files > some_variable
parse some_variable to put the list into one or more variables in the format required
execute your python script with myscript.py $variable_containing_filenames.
That's a very convoluted way to run ./myscript *
:)

Your shell will expand the glob and pass all matching files as a separate arguments to the script.
Image
gm10

Re: How to select many files and pass to a Python script

Post by gm10 »

starplanet wrote: Sat Jan 12, 2019 11:31 pm How can I do this?
Create a launcher pointing to your script, make sure to include %F or %U at the end of the command, then drag & drop onto the launcher.
User avatar
AndyMH
Level 21
Level 21
Posts: 13748
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: How to select many files and pass to a Python script

Post by AndyMH »

That's a very convoluted way to run
I only use bash when I have to :D
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
starplanet

Re: How to select many files and pass to a Python script

Post by starplanet »

Python sounds great...
Is there code that allows me to say "act on selected files"?
I'll find the answer to that if there is.

The other thing is... how would I insert into the right click menu?
gm10 wrote: Sun Jan 13, 2019 6:49 am Create a launcher pointing to your script, make sure to include %F or %U at the end of the command, then drag & drop onto the launcher.
Can you tell me a bit more about making a launcher? I couldn't find any links for this.
Would this involve having 2 windows. And dragging the files from one to the next?

Thanks.
starplanet

Re: How to select many files and pass to a Python script

Post by starplanet »

@gm10
cheeky :)
i didn't know it was common knowledge.
thanks
Locked

Return to “Beginner Questions”