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


Messages In This Thread
RE: How to save latest time stamp in a file? - by snippsat - Jul-03-2019, 06:54 PM

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