允许Shell执行CMD命令

发布于 2025-01-21 16:04:49 字数 913 浏览 1 评论 0原文

我将虚拟环境的权限更改为只读。

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)

现在,我想运行Python代码

import subprocess
subprocess.run("dir")

,但是,这给了我诸如CondriseError和FilenotFoundError之类的错误。

我完成所有VENV文件的权限,因为Shutil可以删除此文件夹,并且我想保护此文件夹。因此,任何人都可以告诉我任何其他方法来保护该VENV免受其他用户的侵害或使用子量表执行CMD命令。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枉心 2025-01-28 16:04:49

当您使用文件权限(例如,将文件读取)工作时,线索通常不是脚本的内容,而是您运行它的方式,依次取决于您的平台:

  • Windows:Windows:运行脚本/作为管理员(右键单击并选择该选项)
  • Linux/unix的程序:使用sudo运行脚本/程序。

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.
丿*梦醉红颜 2025-01-28 16:04:49

更改虚拟环境以阅读&在执行Python代码之前写入,然后将其更改。

Change the virtual environment to read & write before executing that python code, and then change it back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文