简体   繁体   中英

Detecting whitespace in command line argument not working

I am trying to read in a command line argument to a python script and then detect if it has whitepaces or not. I have this so far...

import sys

arg = sys.argv[1]
if arg.isspace() == True:
    print ("Spaces detected")

But if I run the script with the following command then it does not detect that the argument has white spaces...

python myscript.py "my test agrument"

Can anyone see where I am going wrong?

From what I understand you do not want to use isspace but rather know if there are white space inside the string.

Code not tested:

import sys

arg = sys.argv[1]
if " " in arg:
    print ("Spaces detected")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM