Python on windows, shutting down and taking a screenshot - python

I have the following two quesions:
1 I want to be able to shutdown windows xp from python code. I am able to do that by running the following code in the python console:
import os
os.system("shutdown -s -f")
But, if i put the same code in a .py file and try to execute it,it does not work. I get the help prompt for the shutdown command.Any way to fix this ?
2 Is there any way i can take a screenshot of the current screen using python on windows ?
Thank You

There's some code to shutdown windows in this message from the python-win32 list.
You can take a screen shot using PIL's ImageGrab module.

Related

VS Code had a problem in running the code

I tried to run a python program in VS Code. But my program didn't run. The terminal opened and a weird arrow was there in the terminal. This is the screenshot of that.
This is the weird arrow and the program is not running. Any ideas why this is happening and how to fix it?
Thanks in advance.
Firstly, the arrows are included in the default python IDE means that VScode ran the command to execute your code. Give your pc a restart. Now, let us check if python is working or not or VS code is having some trouble. Type the following command in cmd to execute the code-
python "$PATH"
Rember to replace $PATH to the path of the file i.e where your file is stored. For eg. I've my python files stored in D drive in a python folder, so I'll use-
python "D:\Python\Hello.py"
If this works, python is working fine and if not, try reinstalling python and check the box which says Add python to Path or Environment variables. Then open VS code try to run the program again. But click the button only once and be patient because clicking it multiple times causes execute the same command again and cause a problem. It's my personal experience. Wait 5 minutes. Not works. Don't worry, there's a problem with the run extension you are using. I'll recommend the Code runner by Jun han. I personally use it. Type this in the extension search box-
formulahendry.code-runner
Install it and then try again.
Kill the terminal, and retry. If not work, restart the VSCode.

Screenshot with python pyautogui on windows - how to prevent opening shell or cmd

I want to take a screenshot with python using pyautogui. This is my code:
import pyautogui
im1 = pyautogui.screenshot("my_screen.png")
My problem is that this way half of my screen is blocked by the python shell, which pops up when I start the code. So the picture doesn´t really is a screenshot. Prior I have used python.exe to run python files and then the problem was the cmd window poping up. Now I use pythonw.exe, because apparently this prevents the cmd window to show up, but now the python shell window blocks my screen.
So does anyone know how I can take a clean screenshot?
Well, the solution was quite simple. After trying to take a screenshot with pillow I got the same result. So the answear is in the configuration of Spyder, which I was using. Under configuration and "run" you can change where the code will run. And I had chosen that the code shall ran in a external window. So I changed to internal and everything works as it should.
make it sleep for 5 seconds maybe? enough to minimize what is not necessary to capture
import pyautogui
pyautogui.sleep(5)
im1 = pyautogui.screenshot("my_screen.png")

run os.system commands on a new terminal- Python 3

I am running a program which allows me to run terminal commands through my Python code which takes input from the user through the command line. This is the part of the code where I open Google-Chrome
import sys
import os
os.system("google-chrome") #I have Ubuntu 16.04
It opens the browser but the problem is that the terminal on which my python code is running becomes the same as the one where Chrome is running which means that I cannot give further input to my Python code. To solve this problem I need the Chrome to run as a process on a different terminal. I tried using subprocess.call("google-chrome", shell=True) but it did not open it on a new terminal.
How do I make the process run on a different terminal?
can this solve your problem?
os.system('gnome-terminal -x chromium-browser')
Use subprocess.popen("command")
Basically, run the subprocess in the background. & is a shell feature. Use popen instead

How to get Ubuntu Terminal's window handles using Python?

For running few tests on two different serial ports i.e. ttyS0 and ttyS1, I need to work on two different terminal windows or tabs, as the commands I have to run should type simultaneously through Python script.
My prior experience was automating some tasks on Windows OS and with the help of win32gui library for Python it was easy to get window handles.I took help from here for this HWND of each window in Python
But, I couldn't find any library that can help me around here in Ubuntu.
This gives you the name of the current tty in Python:
import os,sys
os.ttyname(sys.stdout.fileno())
If you need it in the Unix terminal then a simple tty is your friend. Then you can do e.g. echo hello > /dev/pts/# to write to your neighboring tab or window.

Python curses Redirection is not supported

I am trying to use Curses in PyDev in Eclipse in Win7.
I have installed Python 3.2 (64bit) and curses-2.2.win-amd64-py3.2. When I input the following testing codes into PyDev:
import curses
myscreen = curses.initscr()
myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()
curses.endwin()
It did not show any syntax error, so I think the curses was installed correctly.
However, when I ran it as Python Run, the output showed: Redirection is not supported. I do not know where this problem comes from. I googled a lot but can't find related information.
Recent PyCharm versions (I am currently running 2017.2, not sure when this option was added, or if it has been there the entire time) have the option "Emulate terminal in output console". Curses works with this option checked.
You cannot expect to use curses with a non-terminal.
Probably you get this because you are running the script from inside an IDE, like PyCharm or any other.
All IDEs do provide consoles that are not terminals, so that's where the problem comes from.
For a Pycharm user the solution given by codeape works fine :
Snapshot
You can't use any IDE to run python files with the curses package. I used to run in pycharm and naturally couldn't run.
Change to the command line to run:
for testing follow my following steps
on desktop open notepad and copy paste the code and save it as filename.py
open command line change directory to desktop use below command cd Desktop and hit enter type python example.py and hit enter, your program will definitely run
My workaround is to create a Run Configuration that calls a curses script. The little overhead is worth not having to switch to the terminal and manually run the script hundreds of times a session. I use Intellij but I imagine the process should be similar in PyCharm.
The desired result is the convenience of a button to run the script:
First create a script that calls the entry script, for instance:
ptyhon name-of-script.py
Then, to create a configuration for each script:
Go to Edit configuration.
Click the plus button and add a Shell Script.
Enter the path to a shell script.
Here is a picture of a directory with a couple of sample scripts.
I use this process to view my progress. My curses scripts are very modest so fortunately I can live without a debugger.

Categories