Python:获取None类型变量

2024-10-01 11:33:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图写一个小脚本同步(镜像)2目录应用程序,我有这样一个配置

app 1                app 2
Network_Devices      Network Devices

Monitoring_Servers   Monitoring Servers

Juice_-_Tomato       Juice - Tomato

Hello_World          Hello World

File_Server          File Server

首先,我得到tool1的目录配置“Linux”

path = '*/some_place/app1' 
for directory1 in os.listdir(path):  #<type 'str'>

在我通过REST请求从工具2获得设置之后

# 
 ... urllib2 library
#     
app2conf = json.loads(urllib2.urlopen(request).read()  #<type 'dict'>

现在我已经准备好执行syn了,如果工具1中的目录存在于工具2中,那么打印“exist”或者打印“doyouwant to create?”我的剧本是这样的:

def sync(directory1, app2conf):    
    for dirin2 in app2conf:                            #<type 'unicode'>
        if directory1 == str(dirin2.replace(" ", "_")):
            print 'Directory already exist'
            return str(dirin2)                         #unicode to string
        else:
            print 'Do you want to create?'
            #
             some code to create directories
            #

for directory1 in os.listdir(path):
    getsync = sync(directory1,app2conf)
    print getsync 
    #
     More code 
    #

问题是,在“print getsync”中,return不会给我任何值。你知道吗

Network Devices
Monitoring Servers
None                   #<type 'NoneType'>
Juice - Tomato
Hello World
File Server

对于测量,我在if之后打印al2值

def sync(directory1, app2conf):    
        for dirin2 in app2conf:        #<type 'unicode'>
            if directory1 == str(dirin2.replace(" ", "_")):
                print directory1
                print dirin2

我得到了这样的东西

Network_Devices
Network Devices
Monitoring_Servers
Monitoring Servers
Juice_-_Tomato
Juice - Tomato
Hello_World
Hello World
File_Server
File Server

脚本正在运行,但我不知道为什么返回给我这些值。你知道吗

谢谢你的时间。你知道吗


Tags: helloworldservertypenetworkfilemonitoringjuice
1条回答
网友
1楼 · 发布于 2024-10-01 11:33:23

您需要返回在else子句中创建的项的字符串。否则sync函数将只返回None(python函数的默认值),并且您将获得分配给getsync的None。你知道吗

相关问题 更多 >