允许Shell执行CMD命令
我将虚拟环境的权限更改为只读。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用文件权限(例如,将文件读取)工作时,线索通常不是脚本的内容,而是您运行它的方式,依次取决于您的平台:
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:
sudo
.更改虚拟环境以阅读&在执行Python代码之前写入,然后将其更改。
Change the virtual environment to read & write before executing that python code, and then change it back.