I changed the permission for the virtual environment to read-only.
VENV_NAME = '._venv'
drive_path = Path(settings.BASE_DIR).drive
def change_permissions_recursive(path, mode):
for root, dirs, files in os.walk(path, topdown=False):
for dir in [os.path.join(root,d) for d in dirs]:
os.chmod(dir, mode)
print(dir, "change successfully")
for file in [os.path.join(root, f) for f in files]:
os.chmod(file, mode)
print(file, "change successfully")
change_permissions_recursive(f'{drive_path}/home/{VENV_NAME}', stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
Now I want to run the python code
import subprocess
subprocess.run("dir")
But, this gives me errors like PermissionError and FileNotFoundError.
I do all the venv files permission because shutil can delete this folder and I want to protect this folder. So anybody can tell me any other way to protect this venv from other users or to execute cmd commands using subporcess.
CodePudding user response:
When you are working on file permissions (like making files read-only), the clue generally is not the content of your script, but the way you run it, which, in its turn, depends on your platform:
- Windows: run your script/program as administrator (right-click and choose that option)
- Linux/UNIX: run your script/program, using
sudo
.
CodePudding user response:
Change the virtual environment to read & write before executing that python code, and then change it back.