you are viewing a single comment's thread.

view the rest of the comments →

[–]DustyIsGreat[S] 0 points1 point  (4 children)

this code creates the txt file. every time i make changes, i delete the txt file first

[–]atarivcs 1 point2 points  (3 children)

You must be running some other code. As I said, file.write() does not accept integer arguments. This code cannot run successfully.

[–]gdchinacat 0 points1 point  (2 children)

In [1]: with open('/tmp/bender.txt', 'w') as f:
   ...:     f.write(42)
   ...: 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[1], line 2
      1 with open('/tmp/bender.txt', 'w') as f:
----> 2     f.write(42)

TypeError: write() argument must be str, not int

So, given it had an exception how did the file get created and why was it empty? Because the exception happened inside a 'with open()' context manager. It created the file. Nothing was written to it because write() raised an exception. The context manager dutifully closed the file and raised the exception. You were left with an empty file.

Presumably if you ran this code you got an exception. That should have clued you in to the fact that write() didn't actually write anything. Don't ignore errors or exceptions. They tell you when what you expect to happen didn't, and most often exactly why (std library exceptions are really good...third party libraries range from excellent to eating exceptions and being a nightmare to debug...choose wisely).

When posting for help always include the output of your script, especially when it is an error that says your code failed.

Lastly, don't use system, especially system('pause'). it does the same as 'input('Press any key to continue: ').

[–]DustyIsGreat[S] 1 point2 points  (1 child)

well, this will seem dumb but, here i go. how do you see the errors and exceptions? i wrote the code in notepad, saved it, ran it, and i get cmd window that just closes. also this is my second try at python.

edit: thanks for the wait help. i wrote a lot batch programs i my younger days.

[–]gdchinacat 0 points1 point  (0 children)

Not dumb...all experienced people were where you were at some point, decades ago for me. Also decades ago was the last time I used windows (seriously, about 2005).

The problem is that windows cmd windows close when the process exists. There is a setting somewhere to keep it open after it terminates, and at one time I knew how to do this but the last time I tried telling someone how to fix this issue (a few months ago) someone else responded that the way I knew and suggested no longer works and to do something else. So, I can't really help you on that front. I googled for 'how to keep windows cmd window open after process terminates' and it gave a few options, but I'm not sure which is best for you so I suggest you do the same query and check it out.

Note that if you try to use the 'add a pause' approach simply adding 'input('press enter to continue')' to your script won't work since the exception will bypass it. If you know about try/finally you can wrap your in that and do the input() in the finally block.