我可以在 Python 中使用多少个 if/else 语句

您所在的位置:网站首页 python中if条件语句可以没有else吗 我可以在 Python 中使用多少个 if/else 语句

我可以在 Python 中使用多少个 if/else 语句

#我可以在 Python 中使用多少个 if/else 语句| 来源: 网络整理| 查看: 265

假设我有一些用 Python 2.7 编写的代码,此代码依赖于一个 if 语句为真,然后使用另一个 if 条件和另一个取决于两个最终条件是否为真我可以继续在我的代码中永远这样做还是有一个限制?无论如何我可以做得更好吗?

#!/usr/bin/python

import os

if os.path.isfile('/tmp/EXAMPLE.txt'):

    if os.path.getsize('/tmp/EXAMPLE.txt') == 0:

        if os.access('/tmp/EXAMPLE.txt', os.W_OK) == True:

            DATA = open('/tmp/EXAMPLE.txt', 'w')

            if DATA.mode == 'w':

                DATA.write('DATA')

                DATA.close()

                DATA = open('/tmp/EXAMPLE.txt', 'r')

                if DATA.mode == 'r':

                    if 'DATA' in DATA.read():

                        print 'File size is no longer zero'

                    else:

                        print 'Data failed to write'

                else:

                    print 'File could not be read from'

            else:

                print 'File could not written to'

        else:

            print 'File does not have write permissions'

    else:

        print 'File has data already'

else:

    print 'File does not exist'



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3