Python语法错误“预期的缩进块”

时间:2015-04-15 16:56:30

标签: python

我想就我的代码寻求帮助,因为它给了我一个我看不到的错误。通常情况下,IDLE会突出显示错误,但这一次,它根本没有给我任何东西,所以我对我的问题位置感到困惑。

也只是一个单挑,我对python很新,并且刚刚在2天前尝试过,所以如果有人能帮助这个菜鸟(我)这个问题,我会感激不尽。

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!")

1 个答案:

答案 0 :(得分:0)

您的代码中存在几个问题:

  1. 您还需要缩进评论
  2. 您需要撤回名称
  3. 您需要放置path.isfile而不是path,isfile
  4. 您需要在第一行后缩进3行
  5. 此处的工作代码:

    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!")