A python script to monitor a directory for new files - TagMerge
6A python script to monitor a directory for new filesA python script to monitor a directory for new files

A python script to monitor a directory for new files

Asked 1 years ago
1
6 answers

The script in question can be easily downloaded, it contains ShellBOT by: devil__ so... guess ;-)

You could use tutorial_notifier.py from pyinotify, but there's no need for this particular case. Just do

curl http://190.186.76.252/cox.pl -o cox.pl.txt
less cox.pl.txt

to check the script.

It looks like a good suite of hacks for Linux 2.4.17 - 2.6.17 and maybe BSD*, not that harmless to me, IRC related. It has nothing to do with Cox-Data-Usage.

Source: link

0

I got some solution,

solution 1 (CPU usage: 27.9% approx= 30%):

path_to_watch = "your/path"
print('Your folder path is"',path,'"')
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
        after = dict ([(f, None) for f in os.listdir (path_to_watch)])
        added = [f for f in after if not f in before]
        if added:
                print("Added: ", ", ".join (added))
                break
        else:
             before = after

I have edited the code, the orginal code is available at http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html

The original code was made in python 2x so you need to convert it in python 3.

NOTE:-

WHEN EVER YOU ADD ANY FILE IN PATH, IT PRINTS THE TEXT AND BREAKS, AND IF NO FILES ARE ADDED THEN IT WOULD CONTINUE TO RUN.

Solution 2 (CPU usage: 23.4 approx=20%)

    import os
path=r'C:\Users\Faraaz Anas Ammaar\Documents\Programming\Python\Eye-Daemon'
b=os.listdir(path)
path_len_org=len(b)
def file_check():
    while 1:
        b=os.listdir(path)
        path_len_final=len(b)
        if path_len_org<path_len_final:
            return "A file is added"    
        elif path_len_org>path_len_final:
            return "A file is removed"
        else:
            pass
file_check()

Source: link

0

The solution to the question wouldn't lie in a python script, this is more of a security issue for the likes of Fail2ban or similar to handle, but there is a way to monitor a directory for changes using Python Watchdog. (pip install watchdog)

Taken from: https://pythonhosted.org/watchdog/quickstart.html#a-simple-example

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    event_handler = LoggingEventHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

This will log all changes, (it can be configured for just file creation). If you want to rename new files to something else, you first need to know if the file is free or any modifications will fail, i.e it's not finished downloading/creation. That issue can mean that a call to that file can come before you've moved or renamed it programmatically. That's why this isn't a solution.

Source: link

0

Hey guys, today’s post is about how to create a watchdog in Python. But what is a “watchdog”?A watchdog is a little piece of software that monitors our filesystem looking for any changes (like the creation, change or deletion of a file or of a directory). When a change occurs, the watchdog report it to us raising a specific event that we can handle.For example, let’s suppose you have developed a program that use a configuration file. Your program could set a watchdog to monitor that file and if the configuration file is modified you could think to reload it and apply the new configuration at runtime, without the need of restarting your program.That’s cool, uh?So, today we will code a watchdog in Python.To code this program we will need an additional module called “watchdog” (wow, who could have guessed it?) written by Yesudeep Mangalapilly, so let’s start by installing it. As always, I raccomand to use virtual environments instead of installing packages system wide. If you want to find out more about virtual environments (that’s probabilly because you haven’t read all my previous post, so shame on you!), just have a look at this article.Now, create your virtual environment (optional but raccomended… at least by me), activate it and install the package watchdog with the following command:
pip install watchdog
Step 1. Import some stuff
1import time
2from watchdog.observers import Observer
3from watchdog.events import PatternMatchingEventHandler
The event handler is the object that will be notified when something happen on the filesystem you are monitoring.
1if __name__ == "__main__":
2    patterns = ["*"]
3    ignore_patterns = None
4    ignore_directories = False
5    case_sensitive = True
6    my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)
Now that we have created the handler we need to write the code we want to run when the events are raised.So, let’s start creating four different functions that will be used when a file is modified, created, deleted or moved.
1def on_created(event):
 2    print(f"hey, {event.src_path} has been created!")
 3
 4def on_deleted(event):
 5    print(f"what the f**k! Someone deleted {event.src_path}!")
 6
 7def on_modified(event):
 8    print(f"hey buddy, {event.src_path} has been modified")
 9
10def on_moved(event):
11    print(f"ok ok ok, someone moved {event.src_path} to {event.dest_path}")
And now, under the creation of our event handler (the code of the previous step) we need to specify to the handler that we want these functions to be called when the corresponding event is raised
1my_event_handler.on_created = on_created
2    my_event_handler.on_deleted = on_deleted
3    my_event_handler.on_modified = on_modified
4    my_event_handler.on_moved = on_moved

Source: link

0

You should consider using inotifywait, as an example:
inotifywait -m /path -e create -e moved_to |
    while read dir action file; do
        echo "The file '$file' appeared in directory '$dir' via '$action'"
        # do something with the file
    done
In case you came here for a simple, fast, handy solution reading the title, you could use watch
watch -n 0.1 ls <your_folder>
I just cooked up this, and see no huge problems with it, other than a tiny chance of missing files in between checks.
while true
do
       touch  ./lastwatch
       sleep 10
       find /YOUR/WATCH/PATH -cnewer ./lastwatch -exec SOMECOMMAND {} \;
done
Ex:
<directory> <file change mask> <command or action>  options
/var/www/html IN_CREATE /root/scripts/backup.sh
You can install it with
apt-get install entr
dnf install entr
brew install entr

Source: link

0

Reading and writing data to files using Python is pretty straightforward. To do this, you must first open files in the appropriate mode. Here’s an example of how to use Python’s “with open(…) as …” pattern to open a text file and read its contents:
with open('data.txt', 'r') as f:
    data = f.read()
open() takes a filename and a mode as its arguments. r opens the file in read only mode. To write data to a file, pass in w as an argument instead:
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
Suppose your current working directory has a subdirectory called my_directory that has the following contents:
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
>>>
>>> import os
>>> entries = os.listdir('my_directory/')
>>>
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']

Source: link

Recent Questions on python

    Programming Languages