Python函数中的语法错误

时间:2014-07-16 01:47:37

标签: python syntax

我已经检查了所有可以尝试修复此代码的内容。它可能只是一些我看不到的傻事,但我今天真的学会了这种语言。我一直得到无效的语法。有人可以查看我的代码吗?

import glob, os, datetime, sys, time, shutil


def rename(dir1, dir2):
    filenum = 1
    date = time.strftime("%d%m%Y")
    fileiter = glob.iglob(os.path.join(dir1, r'*.mp4')
    for filen in fileiter:
        print(filen);
        filename = dir2 + filen + date + filenum + r'.mp4'
        filedir1 = dir1 + filen
        shutil.copy2(filedir1, dir2);
        filedir2 = dir2 + filen
        os.rename(filedir2, filename);
    print("Copying Done!");        
return;

starting_dir = input("Enter Input Directory: ")
if len(starting_dir) == 0:
    print("Actually Enter Enter Something")
    sys.exit()
output_dir = input("Enter Output Directory: ")
if len(output_dir) == 0:
    print("Actually Enter Enter Something")
    sys.exit()
rename(starting_dir, output_dir)

1 个答案:

答案 0 :(得分:2)

在python中,如果你没有指定一个返回它会自动返回None。在您的代码中,return语句的缩进是关闭的。只需删除它即可。这是正确的代码:

import glob, os, datetime, sys, time, shutil


def rename(dir1, dir2):
    filenum = 1
    date = time.strftime("%d%m%Y")
    fileiter = glob.iglob(os.path.join(dir1, r'*.mp4'))
    for filen in fileiter:
        print(filen);
        filename = dir2 + filen + date + filenum + r'.mp4'
        filedir1 = dir1 + filen
        shutil.copy2(filedir1, dir2);
        filedir2 = dir2 + filen
        os.rename(filedir2, filename);
    print("Copying Done!");

starting_dir = input("Enter Input Directory: ")
if len(starting_dir) == 0:
    print("Actually Enter Enter Something")
    sys.exit()
output_dir = input("Enter Output Directory: ")
if len(output_dir) == 0:
    print("Actually Enter Enter Something")
    sys.exit()
rename(starting_dir, output_dir)