How do I modify program files in Python?

前端 未结 4 2150
囚心锁ツ
囚心锁ツ 2021-01-29 16:32

In the actual window where I right code is there a way to insert part of the code into everyline that I already have. Like insert a comma into all lines at the first spot>?

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-29 16:53

    Are you talking about the interactive shell? (a.k.a. opening up a prompt and typing python)? You can't go back and edit what those previous commands did (as they have been executed), but you can hit the up arrow to flip through those commands to edit and reexecute them.

    If you're doing anything very long, the best bet is to write your program into your text editor of choice, save that file, then launch it.

    Adding a comma to the start of every line with Python:

    import sys
    src = open(sys.argv[1])
    dest = open('withcommas-' + sys.argv[1],'w')
    for line in src:
        dest.write(',' + line)
    src.close()
    dest.close()
    

    Call like so: C:\Scripts>python commaz.py cc.py. This is a bizzare thing to do, but who am I to argue.

提交回复
热议问题