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

Operating environment


Raspberry Pi2 + raspbian

In the case of the following code in python code, if the srcpath file is not found, return "" will be displayed.

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

The value received by ~~ return "" seems to be a NoneType object, and the method of determining it was found in the link below. ~~ (Corrected below)

The None Type verdict was found at the link below.

http://python.g.hatena.ne.jp/oneshotlife_tom/20121127/1354009706

If you want to determine if it is None in Python, you can use the == operator.

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

(Addition 2016/03/10)

After reviewing the code based on @ shiracamus's comment, it seems that the following function return is 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

Recommended Posts

python> check NoneType or not> if a == None:> if a is None:
Python list is not a list
Check if the string is a number in python
Python> Run-time argument> Check if -d is attached
python memo-"if not A and B" was "if (not A) and B"
Create a python script to check if the link at the specified URL is valid 2
Create a python script to check if the link at the specified URL is valid
How to use any or all to check if it is in a dictionary (Hash)
How to check in Python if one of the elements of a list is in another list
Python round is not strictly round
What is a python map?
Determine if a string is a time with a python regular expression
When converting a string (a-> b) in Python, which is faster, if statement or dictionary type?
[Blender] Script to check if the selected one is a mesh
If a beginner learns R or Python this December 2019, which one?
Which is better, PyPy or Python?
Delete a particular character in Python if it is the last
Determine if standard output is piped when running a Python script
[Python] return A [or / and] B
Check if you can connect to a TCP port in Python
[Golang] Check if a specific character string is included in the character string
Python does not output errors or output just because the indent is misaligned
[Python] Determine if any coordinate point is inside or outside the polygon
What to do if there is a decimal in python json .dumps
python note: when easy_install is not available
[Python] Name Error: name'urlparse' is not defined
Connect a lot of Python or and and
Check if the URL exists in Python
Python / dictionary> setdefault ()> Add if not in dictionary
What is a dog? Python installation volume
[python] [meta] Is the type of python a type?
If Budokan is a rental office property
Which is faster, Python shuffle or sample?
About February 02, 2020 * This is a Python article.
A story that sometimes does not work if pip is up to date
Separately install a version of Python that is not pre-installed on your Mac
Why Python slicing is represented by a colon (:)
Check if the characters are similar in Python
Python Pandas is not suitable for batch processing
Is sys.settrace, a python genius feature, another language?
Python log is not output with docker-compose up
[Python] Which should be used, return or return None
Create Python folder Check if it already exists
Launch a shell while a Python script is running
(For myself) Flask_AWS_3 (Check if SQL is working)
Judge whether it is a prime number [Python]
python Boolean operation does not return a Boolean value
Tell me what a conformal map is, Python!
What to do if the progress bar is not displayed in tqdm of python
What to do if Python IntelliSense is not displayed in VS Code on Windows
There is a pattern that the program did not stop when using Python threading
[Python] Precautions when it does not work even if TimedRotatingFileHandler is set in basicConfig in python2
A command to check when something goes wrong when the server is not doing anything
Python property description is strange if you look closely (it's not strange if you think more carefully)
python> Check if code is printable> Use ord () / all (c in string.printable for c in hello)