Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python语法错误“;“预期缩进块”;_Python - Fatal编程技术网

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

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

我也是一个新手,最近才试用过python,所以如果有人能帮我解决这个问题,我将不胜感激

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
  • 您需要在第一个if后缩进3行
  • 以下是工作代码:

    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个空格。总之:缩进是一个大的旧的混乱,请将编辑器配置为只使用空格,并将现有的选项卡转换为空格。然后修复所有遗留的缩进问题。