Is there a way to 'hide' the runfile() command so that it doesn't get displayed on the IPython console? It can get really annoying when the file has a long path since it displays the path twice:
runfile('C:/Users/One/Desktop/Training/Week1/Files/file1.py',wdir='C:/Users/One/Desktop/Training/Week1/Files/file1.py')
I agree with you, I hate having a cluttered IPython console .
I found a way while I was trying to find a solution to clear the old text in the console when starting a script:
Define an anonymous function (reference: https://python-forum.io/Thread-Difference-between-os-system-clear-and-os-system-cls) at the beginning of your script to clear the console from text
cls = lambda: print("\033[2J\033[;H", end='')
cls()
The annoying runfile (...) text section will disappear :)
(Spyder maintainer here) This is not possible in our current stable version (Spyder 4), sorry.
This is how I made Alexis' idea work reliably for me. My screen got updated by my code before cls() finished executing, consequently it was blanked over.
from time import sleep
def cls():
print("\033[2J\033[;H", end='')
sleep(0.1)
cls()
Related
As an assignment for one of my classes, the professor had us download a file containing python code as well as a couple of text files that the python code will analyze.
The problem is, when I click to open the python file, the command window opens, then immediately closes. I can get it to stop from closing if I click on it fast enough, but that stops the code from loading, and any keystroke causes it to close again.
It's the same file every other student downloaded and used with no issue. I've tried re-downloading the file, re-installing python, even trying on a different computer. I've also tried opening it by entering the path file name in the command window with no success. Also no luck trying it in Jupyter Notebook or CodeLab. (And of course, no luck experimenting with the slew of possible solutions on previous Stack Overflow, Reddit, etc. questions).
Any ideas on the potential cause and/or solution?
Edit: cause unknown, but the solution was opening the file with Sypder console.
the file closes immediately because It ended and returned 0, to stop that from happening you can add at the end of the python code something like that:
ending = input("press key to exit")
or just import time and use
time.sleep(5)
you can open your file in the cmd/terminal window and you will be able to see the results
I have a python program and it seems that whenever I open it with this line of code active:
content = open('Word_list.txt').read().splitlines() it just closes itself (by open I mean double clicking it from file explorer, works fine every other way). I tried with a small list variable and my program works file when it's like that so I need to convert a 4MB file (4,250,000 lines) into a python list variable. Is there any way to do that and will there be any slowdowns because of what i'm doing here?
Oh, and I should mention that I tried other ways of importing the text file (for loops and whatnot) and that seemed to crash instantly too. Thanks!
Because you are running it through Explorer the window is closing before you can see an error message, so add some error handling code:
try:
content = open('Word_list.txt').read().splitlines()
except Exception as exc:
import traceback
print(traceback.format_exc())
input()
I managed to use Notepad++ and their macro system to put the required " ",'s over all of the words, completing my objecting using another program instead of python.
After looking through pretty much all relevant articles and trying everything, I can't make this work, so many thanks in advance for taking a look.
I wrote the following code to activate second script from the first script:
proc = subprocess.Popen(['python', path_calculator +
'calculator.py'], stdout=subprocess.PIPE)
for line in proc.stdout:
sys.stdout.buffer.write(line)
sys.stdout.buffer.flush()
line = str(line)
line = line[2:]
line = line[:-3]
The point of this code is to get the live output of the 'calculator' in the first script, because other scripts are depending on that output.
The code does provide the correct output, but only when the 'calculator' finishes. It then outputs all data in one go. That's not sufficient. I need the output the moment it comes out of the 'calculator'.
Funny thing is that it actually works on my mac, but I can't get it to work on Windows.
** A few hours later now, I found out that it's not my Mac that it works on, but it's Pycharm (which I use on my mac) that it works on. If I run it in the terminal on my mac it doesn't work either.
Any suggestions?
Finally got colorama working today, and it works excellent when printing strings, but I got the common error everyone seems to get when I attempted to use colorama with input.
Here's my code:
launch = input(Fore.GREEN + "Launch attack?(Y/N): ")
Screenshot of output:
I had this same issue (Python 3.5.4) and, just in case it is not too obvious for somebody else looking at this, you can always rely on the workaround of combining print / input calls where you previously had just an input call:
print(Fore.GREEN + "Launch attack?(Y/N): ", end='')
launch = input()
This should produce the exact same output as in your question, with no extra blank lines and with the code coloring working without the need of importing anything else.
The (small?) disadvantage is that you you will end up with two lines of code where you previously had just one.
On my system, input() works with colors if you add
import sphinx.quickstart
to your module.
So here is the full code.
from colorama import Fore
import colorama
import sphinx.quickstart
colorama.init()
launch = input(Fore.GREEN + "Launch attack? (Y/N): ")
(This leads to two questions:
Why does it not work in the first place?
What is the actual reason? – Someone might like to dive into the sphinx source code.)
N.B. if you run python via winpty from Git Bash, set convert.
colorama.init(convert=True)
Otherwise, you do not get color with the current versions.
To get rid of this problem in the starting of the code add
import os
os.system('cls')
This will clear the screen and hence clear all the external factors blocking the colorama in input.
This works 100% you just need to do it once in the starting of the program [or just before the first use of colorama with input] and after that use colorama in any creative way you want to.
I hope this will help all the creative minds trying to make their codes more colourful
just make sure about the 'autoreset' in init()
init(autoreset=True)
How to clear Python shell ?
I am writing a module in python, I want to save it in a file. what is the best way to do it?
File -> New Window. Put your module in this new window, than save it. To run, just press F5.
Python shell does not get cleared or saved. Perhaps you are using IDLE. It's a confusing piece of software. I'd recommend you to get a real IDE, or at least a proper text editor.
Something that I do in IDLE is print "\n"*100
You can also extend Idle and map that to a key. Read Pythoninstall\lib\Idlelib\extend.txt for details
http://bytes.com/topic/python/answers/737111-extension-idle-clear-screen-but-how-write-screen
If you are just in console, you can do ( which won't work in IDLE):
import os
os.system("cls")
Just copy and paste the code into a new file (In file), and save it. To run, you can go to the run section and select "Run Module", or you can simply press F5.