Change the Windows folder permissions to Read only from Python - python

I have a test case to automate, where in I need to change the folder's permission to read only from Python script. I am using Test Complete tool with python scripting for automating the manual tests.
I am not able to accomplish the tasks, I tried Googling but no luck.

you can try this line of code.
import os, stat
os.chmod(ur"file_path_name", stat.S_IREAD)

Related

Import local ".py" library in powerbi python scripting

I have written a python library (ak_sql.py) to query my sql server and written another ".py" script (query_sql.py) to invoke this library and return data in dataframes.
I want to "import query_sql" in "Python srcipt" in PowerBI, so i can load those dataframes.
I invoked PowerBi from powershell from the root of "query_sql", like below:
C:\Users\akshat\Documents\ak_sql> C:\Users\akshat\AppData\Local\Microsoft\WindowsApps\PBIDesktopStore.exe
However, PowerBI was unable to find the module.
I tried printing "os.getcwd()", this is the output:
C:\Users\akshat\PythonSriptWrapper_{hashkey}
Any inputs on how this can be done?
Try to include the empty file __init__.py in the ak_sql directory and make change in your script on
from ak_sql import query_sql
So seems like it was a PYTHONPATH issue. Was a able to take care of it with appending sys.path:
import sys
sys.path.append(r"C:\Users\akshat\Documents\ak_sql>")

How do I run external programs in Python without using the entire path?

Now, I'm working on a voice controlling assistant application.
When I say "open Google Chrome", it should open Chrome. I do not have any problems with speech recognition but I do have a problem with starting the program.
For example, the following code:
import os
os.system("chrome.exe")
Works fine for the notepad.exe or cmd.exe programs but when I give external programs like chrome, I need to give the entire path.
However, in C# we can only give the direct name like chrome.exe to run the program.
So, my problem is that, is there any ways to start external programs without giving the entire path like in C#?
Giving path to start the program would be a serious problem. Because when we move the program to another computer, we will face many code errors.
Try os.path.abspath
os.path.abspath("chrome.exe")
returns the following:
C:\Users\Yourname\chrome.exe
The PATH system variable governs this inside Python just as outside. If you want to modify it from Python, it's os.environ.
As an aside, a better solution is probably to use subprocess instead, as suggested in the os.system documentation.
The PATH manipulation will be the same regardless.
import os
import subprocess
os.environ['PATH'] = os.environ['PATH'] + r';c:\google\chrome\'
subprocess.run(['chrome.exe'])
... where obviously you need to add the directory which contains chrome.exe to your PATH.
Probably you will want to make sure the PATH is correct even before running Python in your scenario, though.
The first part of this answer is OS specific and doesn't solve it in code or python
Add the path where Chrome exists to your PATH variable (you'll likely need a restart), and then when you execute a command such as chrome.exe it will try to run it from that path location
UPDATE:
If you want to implement a search for the absolute path here are some good resources on path listing How do I list all files of a directory?
But again you'll likely need some educated guesses and will need to do some OS specific things to find it.
The Chrome binary is often installed in the same set of locations
I solved this problem by using C#. Like I already mentioned, we can directly call programs like "chrome.exe" in C#. I created a console application in C# which open Google Chrome via using bellow code. Then I complied that console application as .exe, after that I link that exe file with my python program.
C#
Process ExternalProcess = new Process();
ExternalProcess.StartInfo.FileName = "chrome.exe";
ExternalProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
ExternalProcess.Start();
ExternalProcess.WaitForExit();
Python
import os
os.system('console_application.exe')

How to run the Linux commands which is saved in a text file from python script?

Suppose I have a file 'commands.txt' which has list of linux commands line-by-line (for example: who, pwd, ls, ps, clear etc.,)
I need a python script wherein when run should execute all the linux commands one-by-one in the console/shell.
I am looking for different approaches to do it.
May be one approach I can think off is using os module in Python. (Please correct me if I missed out anything here)
import os
with open("commands.txt") as file:
for line in file:
os.system(line)
Kindly help me with different approaches to this problem.
-TIA
Like #papey said,
Create a bash script (.sh) file, with the correct shabang (#!/usr/bin/env bash).
You could write in your Python script the following then:
import os
os.system("chmod +x myscript.sh")
os.system("./myscript.sh")

How to un-elevate permissions in windows with python

I'm trying to run commands as a particular user rather than an administrator.... seems strange, but roll with me here.
I am running my python script as an administrator, but would like to perform certain functions of the script as an underprivileged user.
For example, something like this would be what I would want to accomplish
from shutil import copyfile
as_user('testuser', 'P#$$w0rd1', copyfile(src, dst))
Other parts of the script would proceed to run as administrator. I've tried using run-as and psexec without any luck as the just end up popping open a new window entirely :|
Other than this, I have no idea how to accomplish this task on windows, with linux this sort of stuff is easy.

Automating Subversion Commands using Python

I am pretty new to Subversion, and not that experienced in Python, but am doing some work with large volume of media-files that need moving around within the directory. Using the Visions GUI, some of the file transfers are taking a very long time, so I'd like to automate these tasks to run over night by storing the actions within a text file and then having a python script act on these overnight?
For example the text file might contain a command such as:
svn mv current desired
How can I send this string to Terminal to execute the command?
You could do os.system call or try using PySVN, which may give you more control in Python over SVN repository you're working with.
The subprocess module is the best way to execute commands. As #Abgan points out, the better way might be to use a subversion library instead.
If you're on Windows, it'd be better to use an SVN library. On Linux/Mac/Unix you could go either way realistically, because these can run a subprocess well - windows doesn't do terribly well at this.
subprocess is indeed preferred over os.system today.
The nice thing about using subprocess.Popen instead of an SVN library (module), is that you don't have to learn two ways of accessing SVN. Your command line SVN knowledge translates directly into your code.

Categories

Resources