detect new or modified files with python [duplicate] - python

This question already has answers here:
How do I watch a file for changes?
(28 answers)
Closed 5 years ago.
I'm trying to detect when a new file is created a directory or when an existing file is modified in a directory.
I tried searching for a script that would do this (preferably in python or bash) but came up short. My environment is linux with Python 2.6
Related Question

You can use gio which is the Filesystem part of GLib (In GLib's python bindings)
import gio
def directory_changed(monitor, file1, file2, evt_type):
if (evt_type in (gio.FILE_MONITOR_EVENT_CREATED,
gio.FILE_MONITOR_EVENT_DELETED)):
print "Changed:", file1, file2, evt_type
gfile = gio.File(".")
monitor = gfile.monitor_directory(gio.FILE_MONITOR_NONE, None)
monitor.connect("changed", directory_changed)
however, your program must be running a GLib mainloop for the events to arrive. One quick way to test that is by using:
import glib
ml = glib.MainLoop()
ml.run()
GLib is a high-level library which is well suited for Applications. You don't have to care about which underlying system it uses for the file monitoring.
I now see you use Fedora Core 2. Really version 2? That might be too old to use GIO in GLib. Pyinotify that has been mentioned might be a better solution, although less portable.

If you're using Linux, you may try pyinotify that acts like an object-oriented wrapper around the inotify(7) system calls. The project website contains quite straightforward tutorials and examples.

If you can use PyQt, there's QFileSystemWatcher that does just this.

There are also Python bindings for gamin.

Related

Manage and import libraries in python installed in libreoffice

I am sorry if this question has already been answered, however it is a topic very little discussed about.
I am trying to run a macro in libreoffice. The macro has been written in python as shown.
import uno, os.path, sys
import pandas
def Bring_from_doc():
doc = XSCRIPTCONTEXT.getDocument()
siz=doc.Sheets
uno, os and sys can be imported without any issue since they are installed in libreoffice python installed folder.
However pandas is not installed and got this error when running script:
This is the directory where libreoffice python libraries are located including uno, os and sys. But pandas and other wanted are not.
My question is: How can be installed pandas and any other required library that can be used by any python script run by libreoffice in a macro?
Thank you!!
On Linux, this is easy. Simply install the library in the system python and it will work from a LibreOffice macro. Verify whether it is python 2 or 3 on your system; for example on Ubuntu, normally I enter python3 as the executable name.
On Windows, this is nearly impossible and I would not recommend it (and have repeatedly stated this on stackoverflow, as it is asked here every so often). Numerous environmental variables must be set correctly and other hacks are required. If you are an expert then it can be done, but since you are asking here, my guess is that you do not have the required skills to make this process go smoothly!
Even if you do get it to work, no one else will be able to use your macro, and you would need to do it all over again if you use a different computer. So I would not recommend it in most cases even for experts.
Alternatives:
Run Linux in a virtual machine.
Install a normal distribution of python elsewhere on your system, and write a normal python script that imports pandas, saving the result for example to an xml file. When that script is finished, run a LibreOffice macro that reads the result file and does not import pandas.
Avoid using specialized libraries at all. This is what I normally do, as the standard python libraries allow you to do quite a bit, maybe not as easily, but you could import some extra code or write workarounds to do what is needed.

Importing .so file in Windows generated in Linux [duplicate]

This question already has answers here:
Using both .so and .dll on Windows
(3 answers)
Closed 4 years ago.
I working on one stuff which is taking a lot of time to execute by using Python code in Windows OS. Hence I decided to use Cython. But in Windows 10 configuring c compiler by using Mingw, felt like a lot of things to done and its not working also. Hence decided to go with Linux to generate .so file and later use that in windows by importing it.
First of all my question, Is that possible to import in windows a .so generated in Linux. If Yes, How can I do that?
Thanks
While it's technically possible, it's almost certainly not what you want or reasonable effort. Some methods that come to mind are using a loader (also known as dynamic linker) that understands the appropriate format, such as the venerable cross-elf (archive.org snapshot), use an emulation layer such as qemu, or a high level virtualization shim like User-mode Linux. In each of these cases, you'd need to run your entire CPython under that same layer, which means it wouldn't have access to Windows features. One of the few projects that did go as far as implementing their own dynamic linker is XFree86.

Preferred Method of Opening a PDF in Python 3

Is there a newer way to open a PDF using an external viewer from Python 3 in Linux other than subprocess?
This sounds like a noobish and duplicate question, but I looked at this question and this question, and all of the answers are over 7 years old and recommended discouraged methods like os.system, old methods like manually creating a subprocess.Popen or Windows-only methods like os.startfile.
So in the time since these questions were answered, have preferred methods of launching a PDF reader from within Python emerged, or are these still the best answers?
Python as of 3.6 still doesn't have a cross-platform way to open files using default programs.
Issue 3177 suggested to add one, but it didn't happen yet.
So:
On Windows, there's a system call for this, you can reach it from Python via os.startfile,
On Linux, there's a command-line tool called xdg-open that does this,
On Mac OS, there's a command-line tool simply called open.
This means that unfortunately you still need to check the operating system and pick the right approach. The correct way to call the command-line tools is using the subprocess module.
This answer provides a code snippet:
Open document with default application in Python

Hide/protect Python code [duplicate]

This question already has answers here:
How do I protect Python code from being read by users?
(29 answers)
Closed 9 years ago.
I am writing code (Python and wxpython for GUI) which will run on Debian OS on Raspberry PI. I want to protect/hide the source code. Is there any way to do it? Probably py2exe, or converting it to a library or something else?
The compiled code (.pyc files) can be used if you wish for others to be able to execute but not to read or modify the source code (.py, .pyw).
Simply:
run your application
then copy all the relevant .pyc files into another folder and you should be able to
run it all from the new location
So long as all the appropriate modules are still able to be loaded, everything will work. This will require the version of python to be the same (can't run .pyc files from python 2.4 with python 2.7 and vice-versa)
The other thing to know is that strings will be preserved. You should open them up in a good text editor (I use vim) and inspect the content if you are worried about what others can see.
py2exe is of course another example, but you lose the ability to have cross-platform code at that point -- and if your application is for the Raspberry Pi -- that won't work.
Since you provided no other information about how you intend to run the code, it's not clear if the source will be a module or intended to be run directly. You should read this post to learn more.

Is there a way to import Python3's multiprocessing module inside a Sublime Text 3 plugin?

I'm making a ST3 plugin on my OSX dev-environment, but according to the unofficial docs:
Sublime Text ships with a trimmed down standard library. The Tkinter, multiprocessing and sqlite3 modules are among the missing ones.
Even if it's not bundled with ST3, is there a way I can still import multiprocessing (docs)? Is there a way I can import it as a "standalone" module inside my plugin dir?
Assuming you intend this program for wide distribution, it's probably unreasonable to expect users to install Python 3 in a standard location in order to use your work. However, depending on Python's redistribution license, you may be able to get away with just bundling multiprocessing with your plugin. If your main my_awesome_plugin.py file is in the root of your plugin directory, put the full multiprocessing directory in as a subdirectory and you should be able to import multiprocessing as mp just fine. Fortunately it's pure Python, so you don't have to worry about platform-specific distributions.
EDIT
OK, my bad. After a little more exploring around the ...lib/python3.3/ hierarchy, it turns out there is a compiled _multiprocessing.so library in there to supplement the pure Python version. So, this will be a little more difficult than just dropping in the multiprocessing directory. I still think it might be doable, as you could theoretically include the compiled library for the three different ST3 platforms, then put in some logic to determine what OS the user is running. However, I'm not as up to date on the Python internals as I would need to be to properly answer this question, so I'll defer to someone else on exactly how that could work.

Categories

Resources