简体   繁体   English

在if else语句中,else块在python中不起作用

[英]In the if else statement, else block is not working in python

if (filenames.rfind(fileName) != -1):
    sftp.chmod(sftpPath + filenames, 0644)
    print (" Permission changed successfully ")
else:
    print (" No file available to process ")

In the above code if statement works but the else block doesnt work(its not printing if file is not available) 在上面的代码中,if语句有效,但else块无效(如果文件不可用,则不打印)

The syntax looks good, there is a probability that content of the variable 'filename' exists in some other string too other than the file you are testing. 语法看起来不错,除了您要测试的文件外,变量'filename'的内容也可能存在于其他字符串中。 You may try to print the filename under if condition and try that out. 您可以尝试在if条件下打印文件名,然后尝试一下。 You can also reduce your search scope by entering a beginning and end index, str.rfind(sub_str, beg=0 end=len(string)). 您还可以通过输入开始索引和结束索引str.rfind(sub_str,beg = 0 end = len(string))来缩小搜索范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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