String comparison fails

前端 未结 1 1902
情深已故
情深已故 2020-12-10 22:56
output = subprocess.check_output(\"./mount.sh\", shell=True)
print output
if output == \"expected_String\":
      print \"Hurray!\"

(The print comm

相关标签:
1条回答
  • 2020-12-11 00:01

    I believe the problem is your output contains additional new line character at the end. You can fix it by calling .strip() to remove those:

    output = subprocess.check_output("./mount.sh", shell=True)
    output = output.strip()
    

    Update: How to Find out If a String Ends with New Line?

    Consider the following interactive session:

    >>> s = '''hello\n'''
    >>> s.endswith('\n')
    True
    
    0 讨论(0)
提交回复
热议问题