0

I am pretty new to programming and one of first things I tried is writing my own file manager (for learning purposes). I use python2.7 and GTK+ 3 library.

While writing my file manager I faced problem: how to react to changes in current folder's files? For example I have home folder opened in my file manager. Then another program creates new file there. File manager should refresh list of files and show actual result. How can I implement it?

Code of my file manager is available on github.

liberforce
  • 11,189
  • 37
  • 48
imslavko
  • 6,596
  • 2
  • 34
  • 43
  • OK, I tried to run separate thread which monitors changes. When changes occur, my thread tries to change Treeview(part of GTK, shows list of files), but fails and application crashes. What would be *correct* way to do this? – imslavko Oct 30 '12 at 08:45
  • need more info. How does it fail? Is there an exception? What line of code does it fail on? If someone tries to help you with your code it is good manners to give them as much info as you can – Sheena Oct 30 '12 at 09:35

2 Answers2

3

GIO is what you should use here. It's provided by the GLib, on which GTK is based. DON'T use polling to do this. Polling is often slow, resources-consuming (CPU, power - as it prevents the CPU from going in deep sleep modes) and has no benefit against an asynchronous API that will just notify you when the content has changed. See also PyGTK/GIO: monitor directory for changes recursively.

You import it that way: from gi.repository import Gio

By the way, I'm removing the PyGTK tag of the question, as PyGTK (which should be used for GTK 2) has been obsoleted in favor of PyGObject (which should be used for GTK3).

EDIT:

Here's a link to the python + GTK3 tutorial.

Community
  • 1
  • 1
liberforce
  • 11,189
  • 37
  • 48
  • thank you for this direction! I am still not sure if I am using PyGTK or PyGObject, most of examples I find in the internet use PyGTK, but I could not establish it on my linux machine (tried to install it with pip to virtualenv) – imslavko Oct 31 '12 at 02:03
  • You find examples on PyGTK because it's has been around for a long period of time. But since the GTK 2 → GTK 3 transition, there have been deep changes, specially with the GObject-introspection work. While a big part of the work needed to have bindings to languages like Python, C++, C# et. was manual, introspection allows to automate that, so that it's easier to automatically generate bindings. This is effective since PyGObject 3, and GTK 3. However that if portability is important for you, there's currently no official installer for GTK 3 or PyGObject 3 on Windows. – liberforce Oct 31 '12 at 09:39
-1

Just check the contents of the directory every few seconds. I'm assuming that you are using os.listdir to get the contents in the first place? Just do that some more.

Alternatively, if efficiency is an issue read on:

look at this for an example, or this for a whole lot more.

Seen this?

These links were gleaned from simple Google and Stackoverflow searches. If they are not informative enough for you please update your question with your requirements

Community
  • 1
  • 1
Sheena
  • 15,590
  • 14
  • 75
  • 113
  • does GTK have something similar to QT's watchdog? Anyway thank you for this direction, I would never think QT or GTK can have already builtin watchdog for this – imslavko Oct 30 '12 at 06:11
  • 1
    Did you look at the links I posted? Come on, try harder! http://packages.python.org/watchdog/ – Sheena Oct 30 '12 at 09:40
  • The downvote comes from the fact that you just randomly dumped the links from the stackoverflow link you gave. You didn't even care to ask if the guy is in fact really using Win32, but gave that to him because that's what was on the other SO thread. Your first solution (polling) is basically the worse advice that could be given to a newcomer, as it should be reserved to only a few, specific cases where it can't be avoided. And in the end, giving google requests is the level of feedback one may expect from Yahoo Questions/Answers, not from stackoverflow. – liberforce Oct 30 '12 at 17:23
  • @liberforce: his requirements were pretty vague, thus the Google thing. People should be specific when asking questions like this. There are waaay too many questions that are about as effective as Google searches due to lack of requirements. Polling, while inefficient, works just fine so I hold that that is valid. Sharing a link to a useful question is not generally frowned upon. So that covers two links. I didn't actually realize that the last link was an answer on the other post, i didn't read the thing, I asked Google and found a solved question that was hideously similar. That's all. – Sheena Oct 31 '12 at 13:50
  • And anyway, you said yourself that there may be cases when polling is the best bet. The lack of specifics in the question does not imply that this is not one such case. – Sheena Oct 31 '12 at 13:54
  • In fact, the pedantic "Come on, try harder" didn't help. As of [polling](http://en.wikipedia.org/wiki/Polling_%28computer_science%29), it is justified when no async API is available, on low level programming, and he's developing a UI, not a parallel port interface. – liberforce Oct 31 '12 at 18:01
  • I didn't know it was common practice to downvote valid answers because you didn't like a comment. The guy was asking to be spoon fed – Sheena Nov 01 '12 at 12:06