khaos64 0 Newbie Poster

I had recently made a .bat file that read a couple lines from file.txt and assigned them as variables then went through the execution of the bat which ended up deleting the original file.txt and writing the new values to a new file.txt.

Now I want to convert this to a .py.

The text file looked like:


The label only being 3 characters and the time in the format of hhmm. and can be anywhere between 0000 and 9959

The .bat assigned them to their own var - then asked for the user to add an additional time in the format of hhmm.

Once the minutes hit +59 it would carry over to the next hour and add the remaining minutes.

Set the variable to the new amount and printed to the file.txt with the new times.

Now being that I haven't worked with python much and haven't looked at in in about 2 years and I remember almost nothing I am having a hard time trying to get started. I'll post what I have so far, but it is far from doing what I want and far from complete,

It can read from a text file with the variables pretty much already set cause of what I found on another post, but doesn’t set it in the correct format (hhmm), asks user for additional hours, again not the right format, and then prints to screen what has been added.


for line in open('File.txt'):
    exec('%s = %s' % tuple(line.split(':', 1)))

x = 'Current '+sec1+' Hours:'+str(h1)
y = 'Current '+sec2+' Hours:'+str(h2)  

##Add hours
print (x)
print (y)

q = input('Add additional hours? ')
if q == 'n':
if q == 'y':

st1 = 'Enter additional time for '+sec1+'(hhmm): '
st2 = 'Enter additional time for '+sec2+'(hhmm): '

Ah1 = int(input(st1))
Ah2 = int(input(st2))

Th1 = h1 + Ah1
Th2 = h2 + Ah2

print (Th1)
print (Th2)

And the text file looks like:

sec1: 'ABC'
sec2: 'ZXY'
h1: 0000
h2: 0000

What would be even better is if there was some way to just keep all internal of the single .py but don't know if that’s possible to rewrite over the variables and start up again with the new values - its probably not worth the effort if it is.

I can also post some of the .bat if that may give you a better idea of what I am trying to accomplish.

Any direction and help is appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.