This question already has answers here:
Request UAC elevation from within a Python script?
(12 answers)
Closed 7 years ago.
I'm writing a python script that will append a line in C:\Windows\System32\drivers\etc\hosts. How do I append a line in this file with administrator permission?
You append to a file by opening it in append mode:
with open('C:/Windows/System32/drivers/etc/hosts', 'a') as f:
f.write('127.127.127.127 static.ak.connect.facebook.com\n')
note: backslash is an escape character, so I use forward slashes, which the msvcrt.dll functions accept just as well
Finally, run your program as administrator in order to have write privileges. My knowledge of this is right-clicking in Windows explorer and choosing 'Run as Administrator'. You could run a command shell (cmd.exe) as administrator and run your python program from there. As far as Windows-specific UAC stuff, you'll need someone who is a Windows system programmer to help with that.
Related
This question already has answers here:
How to run a Python script portably without specifying its full path
(4 answers)
Closed 3 years ago.
I am looking for a way to run a python script from anywhere.
I have seen this solution but as it is based on Linux I am looking for a way on Windows (10).
Basically what I want to do is, execute a script with a given parameter. Because it could be disturbing for the user, I am using a .pyw-file extension to hide the console.
Things that came to my mind:
a PATH-Variable
PowerShell-Script
Batch-file
Sadly I am not familiar/experienced with neither of those, so I can't really tell if these ideas even provide a way to do that.
Any answer is appreciated.
Edit: I would like to make the command as short as possible for the client so it is not necessary to write a novel to exec one simple task.
Another Edit:
I want the user/client to open its cmd/ps and use a command which executes my python-script, which is not in this directory he is, when opening the cmd. So the script is somewhere on his computer, but shall be executable by command from anywhere.
try to create a "code.bat" file with this command "python script.py" inside your .bat file then add the "code.bat" to your path in your environments anytime you want to run the script just type code in your shell.
This question already has answers here:
How to keep a Python script output window open?
(27 answers)
Closed 3 years ago.
In my Python script, I create a .bat file (many actually), and I run them via
os.startfile(blah)
Everything works like expected, however, those terminals die after finishing. I want to keep them open, so that I can type more commands manually in those opened terminals.
How?
You could try using the cmd command to run the batch file, and then use command line arguments for cmd to modify its behavior. For example:
os.startfile("cmd.exe /k blah.bat")
Documentation of the available command line arguments can be found here:
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cmd
This question already has answers here:
syntax error when using command line in python
(7 answers)
Closed 4 years ago.
I have just started learning python and make a python file called "helloworld.py" and store it inside a file called "python py" on the desktop, but this keep poping out whenever I am trying to open it in command line like this:
Can someone tell me what's wrong?
You are in a python shell (we can see that from >>>). Try starting cmd.exe and then run your same command:
python helloworld.py
(from the same path as the helloworld.py file).
When you run Python.exe (as you did), you can not run scripts. This is just for running commands "live". In your shell, try for example print(1). It should work.
This question already has answers here:
Set up Python on Windows to not type "python" in cmd
(4 answers)
How to make python scripts executable on Windows? [duplicate]
(1 answer)
Closed 5 years ago.
I'm reading from a "bookazine" which I purchased from WHSmiths today and its said
during the setup I need to type in these commands into the terminal (or the Command Prompt in my case) in order to make a script without needing to do it manually. One of these commands is chmod +x (file name) but because this is based of Linux or Mac and I am on Windows I am not sure how to make my script executable, how do I?
Thanks in advance.
In the Python documentation there is a small excerpt on this.
On Windows, the standard Python installer already associates the .py extension with a file type (Python.File) and gives that file type an open command that runs the interpreter (D:\Program Files\Python\python.exe "%1" %*). This is enough to make scripts executable from the command prompt as ‘foo.py’. If you’d rather be able to execute the script by simple typing ‘foo’ with no extension you need to add .py to the PATHEXT environment variable.
https://docs.python.org/2/faq/windows.html
Aside from that, like cricket_007 said, you can execute your scripts as
C:\User\YourName> python yourscript.py
You don't have shell scripts on Windows, you have batch or powershell.
If your reading is teaching Unix things, get a virtual machine running (insert popular Linux distribution here).
Regarding python, you just execute python script.py
This question already has answers here:
Pydoc is not working (Windows XP)
(8 answers)
Closed 8 years ago.
I'm working through this "Learn python the Hard Way" book and the book is now saying to run
'pydoc open'
I do this and get the response that pydoc is not an internal or external command etc.
I've trying adding 'C:\Python27\lib\pydoc.py' to PATH and restarting my computer but it still hasn't worked.
Python is probably not in your path. you must add it in your path either by using the GUI or something like this:
set PATH = PATH;/path/to/pydoc/
This is a windows example, but it should not be hard to convert to a *nix version. The export command can be used in that case.