473,651 Members | 3,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python Nautilus script

Hi,

I'm trying to write a python script for Nautilus.
To get the list of files selected in the Nautilus right pane, you use
the $NAUTILUS_SCRIP T_SELECTED_FILE _PATHS environment variable which is
normally available to the script. Actually, it works with bash scripts
but not with python scripts

import os
files = os.environ['NAUTILUS_SCRIP T_SELECTED_FILE _PATHS'].splitlines()

gives a:

File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/UserDict.py", line 22, in __getitem__
raise KeyError(key)
KeyError: 'NAUTILUS_SCRIP T_SELECTED_FILE _PATHS'

Fredrik Lundh explained me that all environment variables are not
accessible from subprocesses of an application.

So my question is how can I get the Nautilus selected files in a python
script?

Thanks,
Michel
--
Michel Leunen
http://linux.leunen.com
Sep 15 '08 #1
10 3445
Michel Leunen schrieb:
Hi,

I'm trying to write a python script for Nautilus.
To get the list of files selected in the Nautilus right pane, you use
the $NAUTILUS_SCRIP T_SELECTED_FILE _PATHS environment variable which is
normally available to the script. Actually, it works with bash scripts
but not with python scripts

import os
files = os.environ['NAUTILUS_SCRIP T_SELECTED_FILE _PATHS'].splitlines()

gives a:

File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/UserDict.py", line 22, in __getitem__
raise KeyError(key)
KeyError: 'NAUTILUS_SCRIP T_SELECTED_FILE _PATHS'

Fredrik Lundh explained me that all environment variables are not
accessible from subprocesses of an application.

So my question is how can I get the Nautilus selected files in a python
script?
There shouldn't be a difference between a shell-script and a
python-script. Environment-variables are a unix-process-thing, and thus
the rules that govern them apply to *all* processes - the shell is one
of these, there is nothing special to it.

If the shell-script gets the variable, the python-script will as well.

Are you sure the shell gets the value? Or is it just silently ignoring a
missing value, and the python-script isn't? According to the docs (I
only googled the variable-name), the variable seems only to be set "only
if local"[1], whatever that means.

https://help.ubuntu.com/community/NautilusScriptsHowto

Diez

Sep 15 '08 #2
Diez B. Roggisch a écrit :
There shouldn't be a difference between a shell-script and a
python-script. Environment-variables are a unix-process-thing, and thus
the rules that govern them apply to *all* processes - the shell is one
of these, there is nothing special to it.

If the shell-script gets the variable, the python-script will as well.
Yes, that's what I thought too but try this: open a terminal and type

$ echo $HOSTNAME

you will get the name of your computer.
Now try this instead:

$ python
>>import os
os.environ['HOSTNAME']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/UserDict.py", line 22, in __getitem__
raise KeyError(key)
KeyError: 'HOSTNAME'
>>>
It appears that's because HOSTNAME is not exported.
But in the case of Nautilus script, how to workaround this issue?

--
Michel Leunen
http://linux.leunen.com
Sep 15 '08 #3
Michel Leunen schrieb:
Diez B. Roggisch a écrit :
>There shouldn't be a difference between a shell-script and a
python-script. Environment-variables are a unix-process-thing, and
thus the rules that govern them apply to *all* processes - the shell
is one of these, there is nothing special to it.

If the shell-script gets the variable, the python-script will as well.

Yes, that's what I thought too but try this: open a terminal and type

$ echo $HOSTNAME

you will get the name of your computer.
Now try this instead:

$ python
>>import os
>>os.environ['HOSTNAME']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/UserDict.py", line 22, in __getitem__
raise KeyError(key)
KeyError: 'HOSTNAME'
>>>
Which is the exact right thing to happen if the HOSTNAME is not exported.

The echo above is executed IN THE CURRENT SHELL environment. If it
weren't - why would there be any distinction between local and exported
variables at all?

If you put

It appears that's because HOSTNAME is not exported.
But in the case of Nautilus script, how to workaround this issue?
I don't know for sure if the shell has something build-in that makes it
spawn shell-subprocesses with a different environment than other processes.

However, if you want you can do something like this:

#!/bin/bash
export VARIABLE_NAME
python /the/python/script.py

You create a shell-script that exports the environment first, and then
invokes python.

Diez
Sep 15 '08 #4
Diez B. Roggisch a écrit :
However, if you want you can do something like this:

#!/bin/bash
export VARIABLE_NAME
python /the/python/script.py

You create a shell-script that exports the environment first, and then
invokes python.
Oh, that's a good idea, I'll try this.
Thanks

--
Michel Leunen
http://linux.leunen.com
Sep 15 '08 #5
On Mon, 2008-09-15 at 22:00 +0200, Diez B. Roggisch wrote:
Michel Leunen schrieb:
Diez B. Roggisch a écrit :
There shouldn't be a difference between a shell-script and a
python-script. Environment-variables are a unix-process-thing, and
thus the rules that govern them apply to *all* processes - the shell
is one of these, there is nothing special to it.

If the shell-script gets the variable, the python-script will as well.
Yes, that's what I thought too but try this: open a terminal and type

$ echo $HOSTNAME

you will get the name of your computer.
Now try this instead:

$ python
>>import os
>>os.environ['HOSTNAME']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/UserDict.py", line 22, in __getitem__
raise KeyError(key)
KeyError: 'HOSTNAME'
>>>

Which is the exact right thing to happen if the HOSTNAME is not exported.

The echo above is executed IN THE CURRENT SHELL environment. If it
weren't - why would there be any distinction between local and exported
variables at all?

If you put

It appears that's because HOSTNAME is not exported.
But in the case of Nautilus script, how to workaround this issue?

I don't know for sure if the shell has something build-in that makes it
spawn shell-subprocesses with a different environment than other processes.

However, if you want you can do something like this:

#!/bin/bash
export VARIABLE_NAME
python /the/python/script.py

You create a shell-script that exports the environment first, and then
invokes python.

Diez
--
Alternatively, export the variable when you create it, in .bashrc or
wherever it is getting created. That's probably the Right Thing to
Do(tm) in this case.

Cheers,
Cliff

Sep 15 '08 #6
>>It appears that's because HOSTNAME is not exported.
>>But in the case of Nautilus script, how to workaround this issue?

Alternatively, export the variable when you create it, in .bashrc or
wherever it is getting created. That's probably the Right Thing to
Do(tm) in this case.

Certainly not, as the OP uses a network monitoring software called
Nautilus - and that communicates state to subprocesses using environment
variables. Nothing to do with .bashrc.

Diez
Sep 15 '08 #7
On Mon, Sep 15, 2008 at 2:00 PM, Diez B. Roggisch <de***@nospam.w eb.dewrote:
>>>It appears that's because HOSTNAME is not exported.
But in the case of Nautilus script, how to workaround this issue?

Alternativel y, export the variable when you create it, in .bashrc or
wherever it is getting created. That's probably the Right Thing to
Do(tm) in this case.


Certainly not, as the OP uses a network monitoring software called Nautilus
- and that communicates state to subprocesses using environment variables.
Nothing to do with .bashrc.
Just to clarify, the OP is talking about Nautilus as in the GNOME file
manager, not some network monitor.
- Chris
>
Diez
--
http://mail.python.org/mailman/listinfo/python-list


--
Follow the path of the Iguana...
http://rebertia.com
Sep 15 '08 #8
Chris Rebert schrieb:
On Mon, Sep 15, 2008 at 2:00 PM, Diez B. Roggisch <de***@nospam.w eb.dewrote:
>>>>It appears that's because HOSTNAME is not exported.
But in the case of Nautilus script, how to workaround this issue?
Alternatively , export the variable when you create it, in .bashrc or
wherever it is getting created. That's probably the Right Thing to
Do(tm) in this case.

Certainly not, as the OP uses a network monitoring software called Nautilus
- and that communicates state to subprocesses using environment variables.
Nothing to do with .bashrc.

Just to clarify, the OP is talking about Nautilus as in the GNOME file
manager, not some network monitor.
Erm, yep - I confused that with Nagios. Please don't ask me why.

The point stands though - .bashrc has nothing to do with that.

Diez
Sep 15 '08 #9
On 15 sep, 21:46, Michel Leunen <mic...@nospam. pleasewrote:
>
But in the case of Nautilus script, how to workaround this issue?

--
Michel Leunen
http://linux.leunen.com
I don't have that issue.
This script works as expected:
---------------
#! /usr/bin/python
# -*- coding: utf8 -*-

import os, sys

#NAUTILUS_SCRIP T_SELECTED_FILE _PATHS : chemins des fichiers
sélectionnés séparés par des retours à la ligne (newline) (uniquement
pour les fichiers locaux)
#NAUTILUS_SCRIP T_SELECTED_URIS : URIs des fichiers sélectionnés
séparés par des retours à la ligne (newline)
#NAUTILUS_SCRIP T_CURRENT_URI : URI de l'emplacement actuel
#NAUTILUS_SCRIP T_WINDOW_GEOMET RY : position et taille de la fenêtre
actuelle
KEYS=("NAUTILUS _SCRIPT_SELECTE D_FILE_PATHS",
"NAUTILUS_SCRIP T_SELECTED_URIS ", "NAUTILUS_SCRIP T_CURRENT_URI",
"NAUTILUS_SCRIP T_WINDOW_GEOMET RY")

ft=open("/home/kaer/stupid.txt", "w")
for key_value in [(key, os.environ.get( key, 'NOT FOUND')) for key in
KEYS]:
ft.write("env(% s): %s\n" % key_value)
file_names=sys. argv[1:]
for index, file_name in enumerate(file_ names):
ft.write("%s: [%s]\n" % (index, file_name))
if os.path.isfile( file_name): os.rename(file_ name, '%03d-%s' %
(index+1, file_name))
ft.close()
---------------
I selected 3 files (created on purpose) in Nautilus.
Those files where renamed and stupid.txt created with that content:
---------------
env(NAUTILUS_SC RIPT_SELECTED_F ILE_PATHS): /home/kaer/baz
/home/kaer/bar
/home/kaer/foo

env(NAUTILUS_SC RIPT_SELECTED_U RIS): file:///home/kaer/baz
file:///home/kaer/bar
file:///home/kaer/foo

env(NAUTILUS_SC RIPT_CURRENT_UR I): file:///home/kaer
env(NAUTILUS_SC RIPT_WINDOW_GEO METRY): 1280x885+0+25
0: [baz]
1: [bar]
2: [foo]
---------------
You can as well use sys.argv[1:] that will give you the list of
selected files.

Hope that helps.
Sep 15 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
4602
by: Erik Johnson | last post by:
This is somewhat a NEWBIE question... My company maintains a small RDBS driven website. We currently generate HTML using PHP. I've hacked a bit in Python, and generally think it is a rather cool language. I've done Perl and like it, there are a few features of PHP I like but overall am not too excited about it. I have found PHP's strtotime() function to be quite flexible and handy and we make liberal use of it. I have not yet really...
2
3254
by: John E. Hadstate | last post by:
Please forgive the heavy cross-posting and feel free to delete inappropriate groups when you reply. I am really at a loss and hope someone has an answer for my problems. Environment: Redhat Enterprise 3, Gnome or KDE, Generic PC, 1.5 GHz P-III CPU, 256 MB RAM, Sun JRE 1.5.0_2. (These problems present on multiple hardware configurations.
2
4898
by: DeepBleu | last post by:
When one is using an HTML form via a web broswer, the user submits the form contents and these are passed to a CGI Python script on the web server. I need to write a client script that connects to a web site, runs a Python CGI script on the web server AND passes a string to the CGI Python script that the Python CGI script can store/manipulate/evalute etc.... I know how to connect to the web site and run the CGI Python script using a client...
52
3748
by: Olivier Scalbert | last post by:
Hello , What is the python way of doing this : perl -pi -e 's/string1/string2/' file ? Thanks Olivier
17
3861
by: Paul Rubin | last post by:
Dumb question from a Windows ignoramus: I find myself needing to write a Python app (call it myapp.py) that uses tkinter, which as it happens has to be used under (ugh) Windows. That's Windows XP if it makes any difference. I put a shortcut to myapp.py on the desktop and it shows up as a little green snake icon, which is really cool and Pythonic. When I double click the icon, the app launches just fine and the tkinter interface does...
9
2073
by: TPJ | last post by:
First I have to admit that my English isn't good enough. I'm still studying and sometimes I just can't express what I want to express. A few weeks ago I've written 'Python Builder' - a bash script that allows anyone to download, compile (with flags given by user) and install Python and some external modules (e.g. wxPython, PyGTK, Numeric...). I use Python every day and when new version of Python (or some external module) is released, I...
47
3466
by: Kenneth McDonald | last post by:
Is there any emerging consensus on the "best" UI for toolkit. Tk never quite made it but from what I can see, both qt and wxWin are both doing fairly well in general. I'm already aware of the licensing issues surrounding qt (fwiw, I think their license fee for commercial use is eminently reasonable), so aside from that, I was wondering if there was any feedback readers could provide on the following: 1) Which plays best with Python?...
6
9921
by: tatamata | last post by:
Hello. How can I run some Python script within C# program? Thanks, Zlatko
7
3879
by: pankajit09 | last post by:
Hello , When I make a FTP connection through Nautilus the connection is still active throughout the day even if I close the window. How to disconnect ?
0
8807
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8584
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5615
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.