I want to use !osm2psql to grab some data in my database. I have to use a Jupyter Notebook.
!osm2pgsql -d mydatabase -U user -W data
But the notebook execute cell and did nothing. -W have to ask password on commandline terminal.
They're is any issue for this case or magik command can't ask a input ?
Related
I want to open a new terminal through python. In that newly spawned terminal I want to execute commands. I've checked multiple posts which are suggested through similar questions but I couldn't find the answer.
I've tried this:
import os
os.system("gnome-terminal -e 'cd /home/john/Documents; echo 'writing to file' > testfile.txt'")
The new terminal spawns but the commands don't get executed in the new terminal.
❯ python3 TestOpenTerminal.py
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
# Error: Failed to execute child process “cd” (No such file or directory)
How can I do this? Thx
The cd command don't work with gnome-terminal command, you need to use the option --working-directory=/path/to/dir.
The -e option is deprecated, you need to use -- followed by the command, like gnome-terminal -- 'command'.
To manipulate files the only way I could make work is using bash and option -c before the command.
So the command you want to do is:
gnome-terminal --working-directory=/home/john/Documents -- bash -c "echo 'writing to file' > testfile.txt"
I really recommend you to use the fabric library to use with terminal commands, it works with all systems and returns the output to you.
I don't know if you really want to open a terminal, but you could just open the file through python and write with it. Try to use the open() and write() functions.
Look closely at your command. Your opening single quote is closed by the single quotes around 'writing to file' and that causes a mess.
os.system("gnome-terminal -e 'cd /home/john/Documents; echo 'writing to file' > testfile.txt'")
Also, the default behavior of gnome-terminal is to close when complete. You can keep it open by editing the profile as follows...
In gnome-terminal, go to Edit -> Profile Preferences -> Title. Click the Command tab. Select Hold the terminal from the drop-down menu labelled When command exits. You should create a new profile for that and execute with
gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -e command
I want to change the IP address of the machine continuously, SO after a lot of search i got this useful link https://github.com/jotyGill/openpyn-nordvpn . I have done the steps as per this URL and i tried the steps in terminal window(Mac) and it is successfully changing the IP.
But the issue is that need to manually run the script again to change the IP, So thought of automating it.
So what i want to do is ,
1. Open an terminal window in mac using Python and execute the following commands,
Enter this command :
sudo openpyn --init
it will prompt to enter password and i have to enter password manually,Is there a way where the program can do this password input automatically using subprocess or any other methods in Python?
After the user name and password is entered ,Need to enter the below command.
openpyn US --area “area”
This will change the IP and need to do the same again.While doing the same either we need to do it from the start or pass CTRL+C to end the current execution and pass
" openpyn US --area “area” "this command again.
I'm trying to execute few statements in terminal window from a python program.
These are the things i have done and no luck .Can anyone please guide me on how to do this?
import subprocess
import os
pid = os.fork()
os.system("""osascript -e 'tell application "Terminal" to do script "sudo openpyn --init;openpyn US --area “area”;os.kill(pid, signal.SIGINT)"'""")
subprocess.call(["sudo openpyn --init"], shell=True)
os.system("sudo openpyn --init")
I don't usually use mac but this might work:
import os
import time
os.system(Command)
time.sleep(0.25)
os.system(password)
Im trying to get python to run a terminal command which will change my desktop wallpaper. Running this command in a normal terminal will change my wallpaper. However when i try to make python run this command in terminal it doesnt work and gives me an error.
I can replicate this error when i open a terminal as root user. I logged in using su and typed my password. Typing in the command then gives me an error and it does not execute. The same when i try to execute the command with python using either the modules os or subprocess.
Is it because my command to change the wallpaper which starts with pcmanfm (which is the window manager) has probblems with its path and when i am root user the path is changed?
How can i make python open a terminal as "normal" user in my case "pi" on my raspberry pi, and run a command in it?
this is the terminal command which changes my desktop wallpaper:
pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg
this is what happens when i run my python code to open a terminal:
root#raspberrypi:/home/pi/Desktop#
instead of normally when it works:
pi#raspberrypi:~ $
here is my python code which is meant to open a new terminal with the command which changes my desktop wallpaper, however i end up as ROOT user as described above and get an error and nothing happens:
import os,random
import subprocess as sub
sub.call('lxterminal -e bash -c "pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg; sleep 3;exec bash"', shell=True)
executing the following from a normal terminal works perfectly:
lxterminal -e bash -c "pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg; sleep 3;exec bash"
this is the result after running the python code, which is the error i always get:
** Message: 12:11:08.734: x-terminal-emulator has very limited support, consider choose another terminal
root#raspberrypi:/home/pi/Desktop#
So how can i make python open a terminal as user "pi" not as root?
or is this not the problem?
thanks!
The problem was calling the python script as sudo.
Opening the python script normally as:
Python3 script.py
Will open a terminal as a normal user and. change my desktop wallpaper,
Whereas sudo python3 script.py opens the terminal as root user and then the command to change wallpaper no longer works.
I'm trying to run a script which opens a new terminal for the user which is sourced from a different file.
My code looks like this at the moment:
os.system("gnome-terminal 'bash -e -c \"source " + path_to_file + "\"'")
Yes it does open a console which also stays open but what I want is also that the console is sourced to the specific file.
I also did a test with running a command with the sourced information dirctly in the above line and it worked. But if I try to start it and do the same it dosn't work.
I hope someone can tell me how I can make it that the terminal will open to the user and that it is sourced.
Please tell me I'm missing something really obvious here:
$ cat ~/bashplay/f
#!/bin/bash
read -p 'RDY> ' x
echo $x
$ ~/bashplay/f
RDY> direct execution
direct execution
$ ssh somehost ~/bashplay/f
indirect via ssh
indirect via ssh
Note the missing "RDY>" prompt when using ssh. I see the same thing in python when using the "readline" package. Anyone know why?
From man bash:
-p prompt
Display prompt on standard error, without a trailing new‐
line, before attempting to read any input. The prompt is
displayed only if input is coming from a terminal.
Use the ssh option -t which forces pseudo tty allocation:
ssh -t somehost ~/bashplay/f