Python语法错误“;“预期缩进块”;
我只是想寻求帮助,关于我的代码,因为它给了我一个错误,我看不到。通常,IDLE会突出显示错误,但这一次,它根本没有给我任何错误,所以我对问题所在的位置感到非常困惑 我也是一个新手,最近才试用过python,所以如果有人能帮我解决这个问题,我将不胜感激Python语法错误“;“预期缩进块”;,python,Python,我只是想寻求帮助,关于我的代码,因为它给了我一个错误,我看不到。通常,IDLE会突出显示错误,但这一次,它根本没有给我任何错误,所以我对问题所在的位置感到非常困惑 我也是一个新手,最近才试用过python,所以如果有人能帮我解决这个问题,我将不胜感激 import time as t from os import path ##dest is the string def createFile(dest): ''' The script creates a text at the passe
import time as t
from os import path
##dest is the string
def createFile(dest):
'''
The script creates a text at the passed in location, names file based on date
'''
date = t.localtime(t.time())
##name=month/day/year
name = '%d_%d_%d.txt'%(date[1],date[2],(date[0]%100))
##if file does not exist
if not(path,isfile(dest+name)):
f = open(dest + name, 'w')
f.write('\n'*30)
f.close()
if __name__=='__main__':
destination = 'C:\\Python34\\My Projects in Python\\'
createFile(destination)
input("done!")
您的代码中有几个问题:
path.isfile
而不是path,isfile
import time as t
from os import path
##dest is the string
def createFile(dest):
'''
The script creates a text at the passed in location, names file based on date
'''
date = t.localtime(t.time())
##name=month/day/year
name = '%d_%d_%d.txt'%(date[1],date[2],(date[0]%100))
##if file does not exist
if not(path.isfile(dest+name)):
f = open(dest + name, 'w')
f.write('\n'*30)
f.close()
if __name__=='__main__':
destination = 'C:\Python34\My Projects in Python\'
createFile(destination)
input("done!")
你在混合标签和空格;由于Stack Exchange在显示制表符时使用了4个空格,因此您的缩进现在很混乱。@DonkeyKong:这是因为这些行上有制表符,但在
if
上没有。您的docstring也需要缩进,它需要缩进到与函数其余部分相同的级别。我建议在您使用的任何IDE中打开“显示不可见字符”。这会让你的生活更轻松。另外,请坚持使用4个空格。总之:缩进是一个大的旧的混乱,请将编辑器配置为只使用空格,并将现有的选项卡转换为空格。然后修复所有遗留的缩进问题。