1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

python > check NoneType or not > if a == None: > if a is None:

Last updated at Posted at 2016-03-09
動作環境
Raspberry Pi2 + raspbian

pythonコードで以下のようなコードの場合、srcpathのファイルが見つからないとreturn ""となる。

def read_fileModificationDate_sendText():
    srcpath="/home/pi/BYOP/send.txt"    
    if os.path.isfile(srcpath) == False:
        return ""
    mddt = time.ctime(os.path.getmtime(srcpath))
    parsed = time.strptime(mddt)
    yyyymmdd = time.strftime("%Y%m%d", parsed)
    return yyyymmdd

return ""で受け取った値は NoneType オブジェクトとなるようで、その判別方法は以下のリンクで見つかった。 (下にて訂正しました)

NoneTypeの判定は以下のリンクで見つかった。

PythonでNoneかどうかを判定するなら==演算子を使えばできますよ。

>>> a = None
>>> if a == None:
... print "None"
...
None

(追記 2016/03/10)

@shiracamus さんのコメントをもとにコードを見直したところ、以下の関数のreturnでNoneTypeになっているようでした。

def read_sendtext():
#    debug_outputDebugString("read_sendtext","Line52 > start")
    srcpath="/home/pi/BYOP/send.txt"

    if os.path.isfile(srcpath) == False:
#        debug_outputDebugString("read_sendtext","Line55 > send.txt not found");
        return
    rdfd = open(srcpath)
    lines = rdfd.readlines()
    rdfd.close()

#    debug_outputDebugString("read_sendtext","Line80 > lines:" + str(lines))

    return lines
1
2
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?