Python Forum
Saving the times a script is run to a file or ...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving the times a script is run to a file or ...
#1
Hi,

I have a lot of scripts from which I'd like to know the amount of times it was run.

I coded a working version a long time ago plus I was even more a beginner than I'm currently. So I'll show you what I have, then perhaps you have a better understanding.

Scope:
- several .py-files
- on network location (local)
- multiple users run those files.
- all of these files run my "log" function at the end of the file.
- log function saves to a network .txt file (simple dump).

Function:
def log():   
    import __main__ 
    project_nummer = projectnumber()
    date = datetime.now().strftime("%d %B %Y")
    dyn_naam = os.path.basename(__main__.__file__)[:-10]
    
    log_path = "T:\\Folder1\\Folder2\\myscripts.log\\"
         
    bestand = log_path + "mylog.txt"
    
    input = dyn_naam + "\t" + project_nummer + "\t" + date + "\n"
    exists = os.path.isfile(bestand)
    
    if exists:
        with open(bestand, "a+") as f:	
            f.writelines(input)	
        return "Log: {} {}".format(log_path, input)
    else:
        print "No log file exists"
Log file:
pdf 3473 19 February 2020
pdf 3473 19 February 2020
stempel 3496 19 February 2020
status 3496 19 February 2020

My questions:
- which is the better format? I now would say json due the library style
- what if two users run (different) scripts at the same time. Could the file be 'open' thus creating an error? If so: can I catch that?
- is this saving to a network location a good approach?
Reply
#2
(Oct-18-2022, 06:25 AM)3Pinter Wrote: My questions:
- which is the better format? I now would say json due the library style
- what if two users run (different) scripts at the same time. Could the file be 'open' thus creating an error? If so: can I catch that?
- is this saving to a network location a good approach?
  • "Which is the better format?" I would not choose json because a json file is supposed to contain one object and is not fit for appending. There are ways to fiddle about with the json principle but I would not do so. I would prefer a csv file.
  • Concurrency and file locking: that is an interesting question. A file object has a "closed" attribute which holds the value True or False, but I suspect this covers only the situation in one program. Not concurrent programs. You could do a test with one program opening a file for let's say one minute, and then a second program trying to open the same file. Does it produce an exception? Then you could use an try ... except construction to wait until the file is not locked anymore. If you are really concerned about this you should consider to use a database. A database will have mechanisms to handle concurrent updates. Even sqlite3 (included with Python) has these mechanisms.
  • I would certainly prefer a network location.
Reply
#3
json or csv comes down to how you want the data organized. Is it just a list of "script, datetime", then csv is the best choice. If it is "script:[datetimes]" then json is better. CSV is always nice for viewing.

If you are worried about multiple computers updating the value at the same time, I would make it a database.
Reply
#4
well,

It's never 'updating' a line.
it's just adding and adding and adding.--> "script X, projectnumber, date"

I could rewrite it to csv, no problem.

Unsure how to test the 'same time' two scripts wanting to write something in the text (or csv) file.
Reply
#5
(Oct-18-2022, 04:26 PM)3Pinter Wrote: Unsure how to test the 'same time' two scripts wanting to write something in the text (or csv) file.
First: if you are planning to have the csv file on a share, then test it on that share.
Create two scripts:
First script:
import time

myfile = r"\\myserver\myshare\myfolder\testbestand.txt"
testbestand = open(myfile, "a")
print("This is the first script", file=testbestand)
time.sleep(60)
print("First script closes file", file=testbestand)
testbestand.close()
Second script:
myfile = r"\\myserver\myshare\myfolder\testbestand.txt"
testbestand = open(myfile, "a")
print("This is the second script", file=testbestand)
testbestand.close()
Start the first script. It will run one minute. Within this minute start the second script. Does it give an error? What are the contents after both scripts have run? And what happens if you start the two scripts from different computers?

I can't test this myself because I don't have Windows.
Reply
#6
Ahhh that's clever! Certainly something I can test.

I did test this one:
- opening the txt file using notepad
- run script.
result: no errors, reopening txt file showed the correct file (with the added line).

Thanks guys!

(Oct-19-2022, 07:36 AM)ibreeden Wrote:
(Oct-18-2022, 04:26 PM)3Pinter Wrote: Unsure how to test the 'same time' two scripts wanting to write something in the text (or csv) file.
First: if you are planning to have the csv file on a share, then test it on that share.
Create two scripts:
First script:
import time

myfile = r"\\myserver\myshare\myfolder\testbestand.txt"
testbestand = open(myfile, "a")
print("This is the first script", file=testbestand)
time.sleep(60)
print("First script closes file", file=testbestand)
testbestand.close()
Second script:
myfile = r"\\myserver\myshare\myfolder\testbestand.txt"
testbestand = open(myfile, "a")
print("This is the second script", file=testbestand)
testbestand.close()
Start the first script. It will run one minute. Within this minute start the second script. Does it give an error? What are the contents after both scripts have run? And what happens if you start the two scripts from different computers?

I can't test this myself because I don't have Windows.
Reply
#7
(Oct-19-2022, 03:28 PM)3Pinter Wrote: I did test this one:
- opening the txt file using notepad
- run script.
Be careful. As far as I know Notepad.exe does not keep the file open. It opens the file, reads the content in memory and closes the file. When saving also the file is kept open short.
Reply
#8
(Oct-19-2022, 04:14 PM)ibreeden Wrote:
(Oct-19-2022, 03:28 PM)3Pinter Wrote: I did test this one:
- opening the txt file using notepad
- run script.
Be careful. As far as I know Notepad.exe does not keep the file open. It opens the file, reads the content in memory and closes the file. When saving also the file is kept open short.

Notepad++ does that to my understanding, didn't realise notepad does too. Good advise. Will do some more testing upcoming weekend (rl. is bugging me atm sigh)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code Assistance needed in saving the file MithunT 0 822 Oct-09-2022, 03:50 PM
Last Post: MithunT
  Saving the print result in a text file Calli 8 1,813 Sep-25-2022, 06:38 PM
Last Post: snippsat
  Trying to determine attachment file type before saving off.. cubangt 1 2,171 Feb-23-2022, 07:45 PM
Last Post: cubangt
  Showing and saving the output of a python file run through bash Rim 3 2,479 Oct-06-2021, 10:48 AM
Last Post: gerpark
  Problem in saving .xlsm (excel) file using pandas dataframe in python shantanu97 2 4,322 Aug-29-2021, 12:39 PM
Last Post: snippsat
  Need help with saving output into an excel file Beyondfacts 4 2,971 Mar-22-2021, 11:51 AM
Last Post: jefsummers
  Automating to run python script 100 times by changing parameters pmt 1 2,616 Dec-29-2020, 10:31 AM
Last Post: andydoc
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,423 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  Copy mp3 file multiple times and rename Mar10 4 3,766 Sep-23-2020, 01:09 AM
Last Post: Mar10
  saving data from text file to CSV file in python having delimiter as space K11 1 2,415 Sep-11-2020, 06:28 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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