Else 的语法无效:3.8
Invalid syntax of Else: 3.8
我是 python 脚本的新手,想知道如何修复下面的代码。我想知道如何解决Invalid Syntax of Else语句,谢谢!
import os
import zipfile
f = open("filelist.txt","w+")
path=("pathtofile")
directory=os.fsencode(path)
filenames = []
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".zip"):
with zipfile.ZipFile(filename, 'r') as zipObj:
contents = zipObj.namelist()
listfile = (str(contents))
remchar = "'[]"
for char in remchar:
a = listfile.replace(char,"")
f.write (a)
continue
else:
pass
f.close()
请记住 python 中的缩进很重要。您的 else:
语句在同一级别上没有匹配的 if
语句。确保正确缩进以实现您要查找的内容:
if filename.endswith(".zip"):
with zipfile.ZipFile(filename, 'r') as zipObj:
contents = zipObj.namelist()
listfile = (str(contents))
remchar = "'[]"
for char in remchar:
a = listfile.replace(char,"")
f.write (a)
continue
else:
pass
else
语句在 if
语句的末尾之外。您必须在结束另一个后立即包含一个 else
。是这样的:
if filename.endswith(".zip"):
with zipfile.ZipFile(filename, 'r') as zipObj:
contents = zipObj.namelist()
else:
pass
如果只写pass
,那么可以直接省略else
。
我是 python 脚本的新手,想知道如何修复下面的代码。我想知道如何解决Invalid Syntax of Else语句,谢谢!
import os
import zipfile
f = open("filelist.txt","w+")
path=("pathtofile")
directory=os.fsencode(path)
filenames = []
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".zip"):
with zipfile.ZipFile(filename, 'r') as zipObj:
contents = zipObj.namelist()
listfile = (str(contents))
remchar = "'[]"
for char in remchar:
a = listfile.replace(char,"")
f.write (a)
continue
else:
pass
f.close()
请记住 python 中的缩进很重要。您的 else:
语句在同一级别上没有匹配的 if
语句。确保正确缩进以实现您要查找的内容:
if filename.endswith(".zip"):
with zipfile.ZipFile(filename, 'r') as zipObj:
contents = zipObj.namelist()
listfile = (str(contents))
remchar = "'[]"
for char in remchar:
a = listfile.replace(char,"")
f.write (a)
continue
else:
pass
else
语句在 if
语句的末尾之外。您必须在结束另一个后立即包含一个 else
。是这样的:
if filename.endswith(".zip"):
with zipfile.ZipFile(filename, 'r') as zipObj:
contents = zipObj.namelist()
else:
pass
如果只写pass
,那么可以直接省略else
。