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
Related
I've only started doing python3 for a paper at university. I don't plan on continuing after this semester is over but I want to do the best I can in this class. However, I am having difficulties with seemingly a basic task.
I've opened a file using f = open() and I've accessed it in the terminal using less. It displays everything out nicely but it doesn't let me close the file or even continuing to code past the printed text.
It just repeats ~ on separate lines and finishes with a highlighted (END)
By typing q, you will be able to exit it
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.
I know from experience that if I try to open the same file in Vim in multiple terminals at the same time, I get an error. (Maybe because of temporary files?)
And I know from experience that if I open a text file in Python and read through it, I have to reset the pointer when I'm done.
But I've found that if I run the same Python script in multiple terminals at the same time, I don't get any error; it just successfully runs the script in both. How does this work? Doesn't Python need to read my script from the beginning in order to run it? Is the script copied to a temporary file, or something?
I know from experience that if I try to open the same file in Vim in multiple terminals at the same time, I get an error.
That's not actually true. Vim actually will let you open the same file in multiple terminals at the same time; it's just that it gives you a warning first to let you know that this is happening, so you can abort before you make changes. (It's not safe to modify the file concurrently in two different instances of Vim, because the two instances won't coordinate at all.)
Furthermore, Vim will only give you this warning if you try to open the same file for editing in multiple terminals at the same time. It won't complain if you're just opening the file for reading (using the -R flag).
And I know from experience that if I open a text file in Python and read through it, I have to reset the pointer when I'm done.
That's not exactly true, either. If you make multiple separate calls to open, you'll have multiple separate file objects, and each separately maintains its position in the file. So something like
with open('filename.txt', 'r') as first:
with open('filename.txt', 'r') as second:
print(first.read())
print(second.read())
will print the complete contents of filename.txt twice.
The only reason you'd need to reset the position when you're done reading a file is if you want to use the same file object to read the file again, or if you've opened the file in read/write mode (r+ rather than r) and you now want to switch from reading to writing.
But I've found that if I run the same Python script in multiple terminals at the same time, I don't get any error; it just successfully runs the script in both. How does this work? Doesn't Python need to read my script from the beginning in order to run it? Is the script copied to a temporary file, or something?
As I think should now be clear — there's no problem here. There's no reason that two instances of Python can't both read the same script file at the same time. Linux allows that. (And in fact, if you delete the file, Linux will keep the file on disk until all programs that had it open have either closed it or exited.)
In fact, there's also no reason that two processes can't write to the same file at the same time, though here you have to be very careful to avoid the processes causing problems for each other or corrupting the file.
terminal is just running the command you said it to execute, there is no pointer or anything
you jus
I have been working on a project and in the end it's suppose to print an output in a notepad file and it works perfectly in pycharm but as soon as I launch my file with python it crash every time I execute the code
I am 100% sure that the problem lies between the line I copied and pasted and I know that if start the project in pycharm with the python console setting active it work too so it may be some right problem? that's the conclusion I came with althought I'm not sure and I don't have any idea how to fix it even with my research. Ho and I,ve checked too and my python is up-to-date
number_of_product = [0,0,0,0]
Total = 0
with open("Bill.txt", "w+") as Bill:
Bill.write("{0} ChocoMilk\n".format(number_of_product[0]))
Bill.write("{0} Katkit\n".format(number_of_product[1]))
Bill.write("{0} N&N's\n".format(number_of_product[2]))
Bill.write("{0} SourJoes\n".format(number_of_product[3]))
Bill.write("Total : {0}$".format(Total))
The result should be 5 line of text written in a notepad file.
I found the problem: I was opening the Pycharm file by clicking "Open with" python3.7 and Windows wasn't giving me all the right I needed.
You need to change the basic application that runs the program from Pycharm to Python itself.
I just downloaded python on a new pc and now whenever I try to create a script the output viewer just closes down immediately when I launch it as a python file.
My original script was
import secrets
secrets.token_hex(32)
but it just closes down immediately. I thought that it was something with my code so I tried to just make the simple "hello world" script.
(print) "I hope this work"
and I had the same result as the first script, the output window opened up then immediately closed down.
I can get it to work using the python shell but I prefer doing all my coding using notepad++ and it would a real pain in the behind if I can't get that to work.
This is expected behavior if you run your scripts as you described, by opening them in Explorer. Your script completes execution in a terminal window, then closes immediately.
If you absolutely insist on running them on double click and still want to see your console, I suggest you create a .bat file at your python path with contents like this:
python -i %1
and them bind your double-click handler to use that file on .py extension. That way, Python will execute your file and go into CLI mode, preserving your terminal window and allowing you to type further commands.
My other guess would be that you want a console plugin to work right within Notepad++, in that case use nppexec: https://sourceforge.net/projects/npp-plugins/files/NppExec/