简体   繁体   中英

Python program won't run correctly when opened from folder or desktop, but works fine when run in IDLE

I have created a simple task-timing program in which the user presses a key to start a timer, and again to stop. The program displays the time elapsed. This all works fine when i run the program, however after this I have the time and task name saved to a file. When run in IDLE (and NetBeans), this save works perfectly. However in the command window, the program closes, not before briefly displaying a traceback error. The data is not saved to the file (which is in the same folder as my program).

Here is the code:

*import time
task = input('What task are you completing?\n')
print('Press ENTER to begin. Afterwards, press ENTER to "click" the stopwatch. Press Ctrl-C to quit.')
input()                    
print('Started.')
startTime = time.time()    
lastTime = startTime

while True:
        input()

        totalTime = round(time.time() - startTime, 2)

        totalTime = str(totalTime)

        seconds = int(float(totalTime))

        minutes, seconds = divmod(seconds, 60)
        hours, minutes = divmod(minutes, 60)
        periods = [('hours', hours), ('minutes', minutes), ('seconds', seconds)]
        time_string = ', '.join('{} {}'.format(value, name)
                                for name, value in periods
                                if value)

        print(str(task) + ' took: ' + time_string)
        time.sleep(2)
        print('Saving to file')
        time.sleep(3)



        writefile = open('Timelog.csv','a')
        writefile.write(task + ', ' + time_string + '')
        writefile.close()
        break
end = input('Press enter to close')

Any help is greatly appreciated.

Is there any chance that you can open a command-prompt/terminal-session and execute from there? That would let you grab the trace.

I'm guessing quant has the right of it and you're running into a access/permissions hurdle of some kind. You can also try saving to a filename that doesn't exist yet; maybe it's just overwriting the file created with the IDE that's is the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM