Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File IO Help
#1
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?
Reply
#2
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-f...ght=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'))
Reply
#3
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.
Gary
Reply
#4
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.
Reply
#5
I agree. I am 72 years old and started programming with assembly. You use what makes you happy.
Gary
Reply
#6
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++
Reply


Forum Jump:

User Panel Messages

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