Python Forum
How to save latest time stamp in a file?
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to save latest time stamp in a file?
#11
(Jul-03-2019, 02:24 PM)redwood Wrote: As you know that I need to run the subprocess.Popen() on the latest file. So I assume that I would have to call it under
"def file_compare" function within the "else" block as it the part that is working on the latest file. Or do I need to use that "schedule" module?
Yes you call it under file_compare function.
You do not need to use the schedule can just run watch_folder.py alone or use Cron.
Example:
import subprocess

out = subprocess.run(['ping', 'google.com'], capture_output=True)
print(out.stdout.decode())
Output:
Pinging google.com [172.217.21.174] with 32 bytes of data: Reply from 172.217.21.174: bytes=32 time=77ms TTL=57 Reply from 172.217.21.174: bytes=32 time=62ms TTL=57 Reply from 172.217.21.174: bytes=32 time=69ms TTL=57 Reply from 172.217.21.174: bytes=32 time=68ms TTL=57 Ping statistics for 172.217.21.174: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 62ms, Maximum = 77ms, Average = 69ms
So if i want to run this in code eg each time a new .txt file is made or can as test use run.txt to activate.
def file_compare(file_stamp, file_name, log_folder):
    # 3 rotations, each with a maximum filesize of 1MB
    logzero.logfile(f"{log_folder}logfile.log",
        maxBytes=1e6, backupCount=3, disableStderrLogger=True)
    try:
        with open(f'{log_folder}stamp.txt') as f:
            old_stamp = float(f.read())
        if old_stamp == file_stamp:
            print(f'No change: {file_name} --> {file_stamp}')
        else:
            if file_name == 'run.txt':
                output = subprocess.run(['ping', 'google.com'], capture_output=True)
                print(output.stdout.decode())
            print(f'New file: {repr(file_name)} --> {file_stamp}')
            logger.info(f'{file_name} --> {file_stamp}')
            with open(f'{log_folder}stamp.txt', 'w') as f:
                f.write(str(file_stamp))
    except OSError:
        with open(f'{log_folder}stamp.txt', 'w') as f:
            f.write(str(file_stamp))
In setup i watch folder eggs and .txt files only.
folder_to_watch = 'E:/eggs'
file_extension = 'txt'
So to test i use schedule(run watch_folder.py every 10-sec) and i make .txt files live.
As soon as i make run.txt the subprocess code will run.
[Image: ktPF6u.jpg]
Reply
#12
Hello snippsat,

Thank you for all your help. So far my tests are as per the requirement. There is one last test remaining, I am on it. That is also a vital part of the entire automation. Will keep you posted.

Regards
Reply
#13
Hello Snippsat,

As per your latest suggestion I did try to execute the "subporcess.Popen" inside the "def file_compare" function within the "else" block as below

def file_compare(file_stamp, file_name, log_folder):
    # 3 rotations, each with a maximum filesize of 1MB
    logzero.logfile(f"{log_folder}logfile.log", maxBytes=1e6, backupCount=3, disableStderrLogger=True)
    try:
        with open(f'{log_folder}stamp.txt') as f:
            old_stamp = float(f.read())
        if old_stamp == file_stamp:
            print(f'No change: {file_name} --> {file_stamp}')
        else:
            Popen(['dpkg', '--install', file_name])
            print(f'New file: {repr(file_name)} --> {file_stamp}')
            logger.info(f'{file_name} --> {file_stamp}')
            with open(f'{log_folder}stamp.txt', 'w') as f:
                f.write(str(file_stamp))
    except OSError:
        with open(f'{log_folder}stamp.txt', 'w') as f:
            f.write(str(file_stamp))
But It is doesn't seem to recognize my the file that I am adding to the folder, below is the error output:
==================
sudo python3.6 watch.py
New file: 'corp-license-hosting-abc_0.3-2_amd64.deb' --> 1562840308.0855987
corp-license-hosting-abc_0.3-2_amd64.deb --> 1562840308.0855987
dpkg: error: cannot access archive 'corp-license-hosting-defacto_0.3-2_amd64.deb': No such file or directory
===================

if I see '/tmp' , I see those files there, and that is the very reason why we could see other message in the error for the detection of the new file

Note:
1)I did try to change the permission on the file, but by the time it is done, the program consider it old file.
2) I am executing the program as sudo. So, the permission issue shouldn't be there.

My another doubt is, if we have to mention the "subprocess.Popen" inside the "def file_compare" function, then what is the purpose of having "newest_file" variable under
"if __name == '__main__'"?

I did use the subprocess on the newest_file. So, following observation:

When I execute the program, and if there is a new file, then it recognizes it and dpkg happens on that new file. But if I execute the program twice, I do get the message "No change", but the "subprocess.popen" still execute the "dpkg" on the file.
So, I believe that we cannot use the "subprocess.popen" under "if __name == '__main__'". Please correct me if I am wrong. As, it is also one of my requirement, wherein, if the '.deb' file is old then "subprocess.popen" is not suppose to execute the 'dpkg', even if the corn job is executed 10 times. In other word, if the program finds the new file, then only "subprocess.popen" is suppose to work.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 337 Jan-24-2024, 06:28 PM
Last Post: frohr
  how to save to multiple locations during save cubangt 1 561 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Filename stamp script is needed ineuw 11 4,621 Sep-12-2023, 03:05 AM
Last Post: ineuw
  save values permanently in python (perhaps not in a text file)? flash77 8 1,249 Jul-07-2023, 05:44 PM
Last Post: flash77
  Save and Close Excel File avd88 0 3,083 Feb-20-2023, 07:19 PM
Last Post: avd88
  Save multiple Parts of Bytearray to File ? lastyle 1 962 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  How to map by time stamp and name SriRajesh 0 1,152 Apr-09-2022, 12:49 PM
Last Post: SriRajesh
Sad Want to Save Print output in csv file Rasedul 5 10,993 Jan-11-2022, 07:04 PM
Last Post: snippsat
  Get latest version off website and save it as variable [SOLVED] AlphaInc 5 1,988 Nov-14-2021, 09:00 PM
Last Post: DeaD_EyE
  How to save Matplot chart to temp file? Morkus 2 4,542 Jun-12-2021, 10:52 AM
Last Post: Morkus

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020