Python Forum
File IO Help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: File IO Help (/thread-14705.html)



File IO Help - jarrod0987 - Dec-13-2018

I have found that running a program in IDLE is slower then running it by right clicking and openwith Python. Also, that second way let's me run multiple instances. The following code works fine in IDLE..

def main():
    var = 123

    # Writes
    fo = open('test.py', 'w')
    fo.write('num = ')
    fo.write(str(var))
    fo.close()
However it crashes when I try to run it in the openwith way. No error that I can see.
Can someone tell what is happening or maybe how to write a file while using the openwith way?


RE: File IO Help - Larz60+ - Dec-13-2018

You should choose another IDE. Python.org includes Idle with python, but it is a poor IDE and really shouldn't be used.
Although choice of an IDE is personal, VSCode is highly recommended, easy to install and learn, and offers many extensions.
You can install and try it out using this installation procedure: https://python-forum.io/Thread-VS-Code-from-start?highlight=VSCode

as an FYI, a better way to open and read files is with the context manager 'with' statement:
def main():
    var = 123
 
    # Writes
    with open('test.py', 'w') as fo:
        fo.write('num = {}'.format(var))
No close necessary
this can be simplified further:
def main():
    # Writes
    with open('test.py', 'w') as fo:
        fo.write('num = {}'.format('123'))



RE: File IO Help - gehrenfeld - Dec-13-2018

I use PyCharm it is pricey but one of the best I have tried.

I have used Visual Code and Notepad++ both are good for a quick edit.

I use Anaconda3 that has the Jupyter editor which I find very good for learning and testing Python code.


RE: File IO Help - Larz60+ - Dec-13-2018

Quote:I use PyCharm it is pricey but one of the best I have tried.
PyCharm has a free community edition which is fine if not in commercial setting.

I used PyCharm for a long time (years), but found VSCode more suitable for my own work.
As I stated, it's personal.


RE: File IO Help - gehrenfeld - Jan-27-2019

I agree. I am 72 years old and started programming with assembly. You use what makes you happy.


RE: File IO Help - Larz60+ - Jan-27-2019

Quote:I agree. I am 72 years old and started programming with assembly. You use what makes you happy.
I'm in same age group!
70++