Problems with opening html - python

This must be the most weird and confusing problem I've ever seen. So I've been trying to open an html file with python using the:
import webbrowser
webbrowser.open_new_tab()
method, at first it worked as intended but then problems started to appear. When I ran the program the file would open, but after 1 second it would close. This would always happen when I ran the program with VS code but when I ran the python program in IDLE (v3.10.0) the program ran just fine. I'm out of my depth here and I have no idea what could be causing this problem.
def visit_website():
print('Would you like to visit our website ?')
def_a=input()
if def_a=='yes' or def_a=='Yes' or def_a=='ok' or def_a=='Ok' or def_a=='OK':
webbrowser.open_new_tab('demofile.html')

Related

Scripts overlapping while running in Pycharm

screenshot of my pycharm interface
Hey everyone. When I run this simple code (the tab 'youtubeyf.py
is to print 'hello world'), it produces the intended result but also another script (with the tab name "yfinance.py") result as well. In fact, the "yfinance.py" script doesn't need to be open in order for that result to appear too. It is almost as if it runs in the background, parallel without being open.
Goal: I want to run "print 'hello world'" without the dataframe from the other script appearing.
Problem: the dataframe is appearing without actually running or being open.
Troubleshoot attempts so far: I have "Alphabetted" and searched on StackOverflow, JetBrains on topics regarding reconfiguring the "run configurations", "parallels", and nothing yet.
Thank you for your time.
Edit 1: this does not solve my problem. for one, my issue isn't importing. in fact, everything is "running" smoothly, but that the results are two in one. It is analogous to two chefs preparing one meal each, say shrimp lo main and pizza, and then placing both meals onto one plate. I don't want that; I want to have shrimp lo main on Tuesday night, and then pizza Wednesday.
When importing yfinance, it could be possible that in that module, your code is structured such that when you import the module it runs a function or other code. In yfinance, you should delete any extraneous function that doesnt need to be run everytime you import yfinance somewhere else.
Edit: what you can do, if you dont want to change much of the structure yfinance.py, is wrap all the code that runs in that python file inside a main function and add the following:
if __name__=="__main__":
main()
This makes it so that python only runs main if you're actually running yfinance.py , and doesnt run main when you're importing it.

The IDLE editor in Python will not run my code when I do not save it

I have written code in a New file window in IDLE.
When I run the code there is no output.
Instead a dialog box appears showing a window accessing Python Folder 37-32.
When i closed the dialog box and the file I tried to create a new simple code below but I when I ran the code I got the same Dialog box.
What is wrong?
sum = 2+3 print(sum)
I have attached a screenshot showing the code and the dialog box that appears when the Module is run
Before you can execute your code, your first need to save the file. Thats the dialog box that popped up.
You should have seen a popup box like below. Did you? Is is unclear?
Save Before Run or Check
? Source Must Be Saved
OK to Save?
[OK] [Cancel]
One reason to require saving is that exception tracebacks refer to the file and line of lines that lead to the exception.
If you had saved, sum = 2+3 print(sum) would be a SyntaxError.
You can run single statements in Shell without saving.
The RESTART lines says that the shell re-initialized the environment for executing your code.
You should normally not save your code buried in the installation Scripts directory. Better to make a directory in your user directory, for instance, C:/Users/yourname/py/.
Yes, one should usually open a new question for unrelated questions. But without access to your machine, it is hard to know what happened with 'new'. It may be that IDLE could create a file under .../appdate/.../Scripts/, but your code cannot. If the open call did not raise and exception, it was likely created somewhere. Until you are more experienced, better to use absolute paths, such as C:/Users/yourname/py/new.txt.

Python file dialog freezing issue while using selenium

In order to print stuff on campus, I need to login to the webpage and submit the printing job manually on a browser, but I wanted to submit a job locally. So I wrote a small python script that uses Selenium to automate print job submission. I've verified that it works, but there's one thing that bugs me. Even after I select a file to print, the file dialog stays there until the actual code runs to the end.
The structure of the code is like the following.
1. Enter information on terminal (username, pw, which printer to use, how many copies, etc.).
2. Call tkinter.Tk().withdraw() to select a file (after selecting a file, "Submitting a print job..." is printed, as shown on the screenshot)
3. Do the actual Selenium job using information I collected above to submit a job
How can I make that file dialog disappear as soon as I select the file? Is it an ascyncio problem?
I noticed that many other people were experiencing a similar issue. While many had solved the issue by adding Tk().update() before or after askopenfilename() line, my problem was only gone when Tk().update() line was added both before and after the askopenfilename() line. FYI, I'm running the script on macOS with python 3.7.

Remaking the Crazy PC Program, But how to run simultaniously?

It has been a while since i have last visited stackoverflow, But i am happy to be back.
Now. Maybe some of you remember the 'Crazy PC "Virus"', (Program that constantly opens and closes your disk drive and maybe plays a song and shows a picture, But is harmless and should be made to be easy to close)
Well. For the next april fools i wanted to use this, but i cant find it.
So i decided that i needed to make it myself!
I have found some code to control the disk drive,
import time, pygame.cdrom as cdrom
#time.sleep(5)
while 1:
cdrom.init()
cd = cdrom.CD(0)
cd.init()
cd.eject()
cd.quit()
cdrom.quit()
time.sleep(5)
(Thank you shubham1810)
But i am not certain if it is python 3 and it is a while, meaning (For all i know, Still a noob at code) that it wont run any other code until the while ends.
But as stated before i also want a picture to be on the screen and a sound file to play...
Have you guys got any ideas? Because i dont :(
-Natan
EDIT 1:
Ok, i have now finally been able to install pygame, and this code works on python 3. What puzzles me is that it opens the drive one time and then doesnt close it again. Does anyone know how to do this?
EDIT 2:
I have decided that this wont work and have since found a working vbs script for the cd drive opening and closing. The script is:
Set oWMP = CreateObject("WMPlayer.OCX.7")
Set colCDROMs = oWMP.cdromCollection
do
if colCDROMs.Count >= 1 then
For i = 0 to colCDROMs.Count -1
colCDROMs.Item(i).Eject
Next
For i = 0 to colCDROMs.Count -1
colCDROMs.Item(i).Eject
Next
End If
wscript.sleep 5000
loop
I will call it within python and then the picture and the song, but that still doesnt solve my problem. I still need and a picture and a song to play in the background (So without the user finding them).
Any idears?

Unable to modify python script in Windows

I'm currently using PyCharm and Sublime Text in Window to develop some Python script, but this morning something quite strange happens.
I am changing my code and testing it by running my code over test input. It is supposed to change the output text quite a lot. The output text at the end did not change at all. Then I try commenting out my output function, simply pass through the reading script. I thought "Now it should print nothing". But it prints the same output as yesterday, as if I never modified it today.
Any suggestion?
It turns out it's because my colleague has pushed an unexpected change up to repo last night...I always pulled before I end the day. The text output was forced by his logger:)
Mystery solved! Thanks for reading guys.
I've had exactly the same thing happen with Sublime. In my case I had moved / renamed the file after closing sublime. I reopened sublime some time later which reopened the old version of the file because I had left the file open when I last closed down sublime.
Have you tried closing the file and then opening it again from windows i.e. right-click => open with Sublime Text, to make sure you're editing the correct version?

Categories