社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Rishi Bansal

Rishi Bansal 最近创建的主题
Rishi Bansal 最近回复了
7 年前
回复了 Rishi Bansal 创建的主题 » 如何使用uu name_uuuu==”uuuu main_uuu“来编写python脚本[副本]

python中的每个模块都有一个称为 名称 . 价值 名称 属性为' 主要的 '当模块直接运行时。否则 名称 是模块的名称。

简而言之就是一个小例子。

#Script test.py

apple = 42

def hello_world():
    print("I am inside hello_world")

if __name__ == "__main__":
    print("Value of __name__ is: ", __name__)
    print("Going to call hello_world")
    hello_world()

我们可以直接执行

python test.py  

输出

Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world

现在假设我们从其他脚本调用上面的脚本

#script external_calling.py

import test
print(test.apple)
test.hello_world()

print(test.__name__)

当你执行这个

python external_calling.py

输出

42
I am inside hello_world
test

因此,上面的内容不言而喻,当您从其他脚本调用test时,if循环 名称 在test.py中将不执行。

7 年前
回复了 Rishi Bansal 创建的主题 » 如何在python子流程中由其他用户运行文件

你可以尝试下面的解决方案

def change(user_uid, user_gid):
    def result():
        report_ids('starting demotion')
        os.setgid(user_gid)
        os.setuid(user_uid)
        report_ids('finished demotion')
    return result

def report_ids(msg):
    print 'uid, gid = %d, %d; %s' % (os.getuid(), os.getgid(), msg)


import subprocess
filename = '/mount/test.sh'
#mention UID and GID here
p = subprocess.Popen(filename, preexec_fn=change(1000, 1000))
result = p.wait()
print(result)

注意:我用check_输出代替popen。因为我不能测试,如果有打字或错误,请原谅。另外,检查p.communicate是否与check_输出一起工作。

7 年前
回复了 Rishi Bansal 创建的主题 » 无法使用python从json文件打印项

请使用“求救信号”

#!/usr/bin/python

import json


with open('sample.json', 'r') as f:
    r107sData = json.load(f)

for r107s in r107sData:

    print(r107s['feedback']['sos_signal'])

请注意我的json文件名有点不同