How to run a Python script with higher permissions? - python

I am trying to make a basic program to backup one folder from my memory stick when it is plugged in, (I know there are already programs that can do this but that is no fun!) but am having trouble with permissions.
from shutil import copy2
copy2('F:/Python/Library', 'C:/Users/Torran/Desktop/Python')
This is all I have so far, as I want to get the copying part working before doing the detecting when it is plugged in part. When I run this, however, it keeps giving me a PermissionError...
PermissionError: [Errno 13] Permission denied: 'F:/Python/Library'
I know that a Python script can only access folders in the same folder it is saved to, however this doesn't really help as I need to copy a folder from my memory stick and paste it into a folder on my desktop, so I need a way to give this script access to folders outside the folder it is saved to.

After trying it myself, I found out the problem. You are using the shutils.copy2(src, dst) function on a folder, not a file. src has to be a file. If you are trying to copy a folder to a destination folder, you need to be using shutils.copytree(src, dst).
You end up getting the permission error because shutils.copy2()expects a file.
As for the underlying question of your issue for copying a folder to a destination, please read this for a few different ways to handle this issue.

I would suggest taking a look at "Run python script as admin in Windows", as this answer explains how to force extra admin permissions. Try that, then should it not work the problem will likely lie with the command as cmpgamer says.
By the way, welcome to Python and programming in general! It is a great world to get into as it lets you achieve so much in so many fields. Python is "the" language to know right now as it is very powerful and quick to dev. Have you tried working with the Raspberry Pi? You can do some very fun Python projects on them! Backup, as you are describing, can be achieved with Windows Shell scripting, whereas you can do AI in python!

Related

How do I access other files included in the zip package when using Appium Python onAWS Device Farm?

I've looked everywhere for this. There seem to be some solutions for Java but not for Python Appium. Basically, I would have a main python script (i.e. *_test.py), but then I would try to access other files in the same directory from this script, using the "with open" expression, such as as follows:
import yaml
with open('desired_capabilities.yaml', 'r') as desired_capabilities_file:
desired_cap_data = yaml.safe_load(desired_capabilities_file)
This works fine when running the script in my local machine, but when I try to run it on AWS Device Farm, it breaks down, producing this error:
FileNotFoundError: [Errno 2] No such file or directory: '/desired_capabilities.yaml'
It seems like AWS Device Farm does not just extract everything in one folder and then run the test scripts. I saw in a solution for Java that I cannot simply access the file like this but there was something about accessing the current thread or something like that, but I am not familiar with how that can be done. Or maybe there is a simpler way to make this work? I have been struggling with this for half a day. Any help will be really appreciated!
*I'm using Python 3.7

VS Code: "NO FOLDER OPENED"

This may seem trivial, and I admit that it is more a question relating to ease of access in VS Code.
My VS Code window shows "NO FOLDER OPENED". How do I fix this?
Up until the other day when I set VS Code as my default app for .py files (this is the problem), I could see any and all folders, sub-folders, and .py/.ipynb files on my Explorer tab. I am both new to Python and VS Code. I have tried these things to fix my problem:
Attempted to reset to all of microsoft's default app associations--the aforementioned files still open with VS Code.
Attempted to change each file's "Open with:" path (by accessing its properties)--it gives no other option, since PowerShell and CMD are not "apps".
Found this that got close but did not target my specific problem.
Found another article which refers to Windows 7--I didn't read it after seeing this.
I have done various other things, but I think you get the point; I'm at a loss!
This problem seems annoyingly easy to fix. I would rather see if there is some type of setting, in VS Code or Windows, that I am overlooking before searching for a complex work-around.
I'm also new to Stack overflow. I hope this is a "valid" question.
It's important to keep things organized even when you are new to coding. In VScode it is pretty simple to do that. I suggest you to make a folder for your projects and also for other purposes like practicing on examples.
Lets say you created a folder on desktop named Python, and you keep all your .py files in there, then you don't have to open each of the python files by double clicking it, it's a wrong practice. Instead, open VScode application from the start menu or desktop, then go to file > open folder > Python(the python folder you can create on your desktop). This way it opens a workspace(Folder) where you can work with different Python files or any other files.
With Ctrl key pressed, press O and then K. Then, select the folder where your python file is located.

Reliably set working directory for python application

I am making a small application that I will invite some users to test for me. I have been developing in PyCharm, and running the program from the dev directory, and the issue of a working directory just recently came to my mind.
The program and all files are contained in the project directory. Naturally, I want the working directory to be the project directory, regardless of where the user decides to store the program.
What is the most reliable way of achieving this? I was thinking something like this
import os
dir_work = os.path.dirname(os.path.abspath(__file__))
and then set all paths relative to dir_work. Is there a better way? I have also been thinking of making a setup script that the user can edit, and that when run hardcodes some stuff into the source code. But if my problem can be achieved without user intervention, that would definitely be the best solution.

How to Give Python Permission to Write Files

I'm learning Python and don't quite have the vocabulary to describe this. However, I can't seem to save files created in Python to my Window10 computer. I discovered this while seeking a help to try to get a file to save in Pandas. I then discovered the same problem when creating a db using SQLITE3 the script seemed to have fun but no database files appeared.
Does anyone know how to fix this? FYI I've got a dual boot Ubuntu machine, I can save files via Python in Ubuntu but really need it to work on my windows machine too.
I am running python via Jupyter Notebook.
I had to make a couple changes to the code snipped that you linked in order to get this to work.
A difference between windows and linux is the file path deliminator is a forward slash:
df.to_csv("tests/ysi_test_files/filehere.csv", index = False)
If you want a hard absolute path to a file, then do something like:
df.to_csv('C://Folder//myfilename.csv', index=False)
Again, if you copy the folder path from a windows folder you will get the backslashes instead of forward slashes. You will need to change those in your code to save the file:
C:\Users\myuser\Desktop\python\
to
C:/Users/myuser/Desktop/python/
If you are running the python script from command prompt then right click on it and run as administrator should solve the issue.

Install program to Raspberry Pi

I am taking a computer science program in University. There is a style check program we use to make sure our C code is formatted the way the teachers want.
The procedure I have to follow currently is copy two python files into the directory of the file I want to check. The files are named "cpplint.py" and "styleCheck.py". So after copying them in I execute chmod u+x styleCheck.py and then I can run it with ./styleCheck.py.
So my question is: is there a way to install the style checker on my Raspberry Pi so that I don't need to do all the copying and pasting? It would be nice just to be able to run the style checker with the ./styleCheck.py command in any directory and avoid all the repetitive cutting and pasting.
What would I suggest ,although not a direct answer to your question, is tinkering a bit with the code and see if you can pass as an argument the location of your files. That way you could just go into the directory of you styleCheck.py and do "./styleCheck.py destination/of/your/file" without having to copy anything. This will also make you feel good about improving a university piece of code.
Keep in mind that you should do that only if you are familiar enough with python so that you do not break the actual checker.
Another option is put the files in a folder, do your chmod once to make them executable and then write a quick shell script that takes the file location as a parameter. Then have it copy cp the files into that folder, run your tester and delete them upon completion.
This is of course option B, I would simply do what Martin said.

Categories