debugging python def create_file_if_not_exist(file_name:String):SyntaxError:冒号附近语法无效

f0brbegy  于 2023-03-23  发布在  Python
关注(0)|答案(1)|浏览(103)
File "report.py", line 17
12:05:47      def create_file_if_not_exist(file_name: str):
12:05:47                                            ^
12:05:47  SyntaxError: invalid syntax

当我运行python www.example.com时出现此错误report.py
有人能帮我找出我做错了什么吗
我试着删除:def create_file_if_not_exist(文件名:str)“但错误传递到下一行

def set_logging():
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s %(levelname)s %(message)s',
        handlers=[
            logging.FileHandler("debug.log"),
            logging.StreamHandler()])

def create_file_if_not_exist(file_name: str):
    if (not path.exists(f'{sys.path[0]}/{file_name}')):
        logging.info(f'{sys.path[0]}/{file_name} not exist.')
        data_file = open(f'{sys.path[0]}/{file_name}', 'w+')
        csv_writer = csv.writer(data_file)
        values = ["Date", "Test Level","Total","Fail","Pass","Elapsed","Jenkins Build","Pass%","Build URL"]
        
        csv_writer.writerow(values)
        logging.info("Header added to the file")
        data_file.close()
        logging.info(f'{sys.path[0]}/{file_name} created.')

if __name__ == "__main__":
    try:
        set_logging()
    
        logging.info("---- Fetching csv file ----")
        file_name = "report_FT.csv"
        create_file_if_not_exist(file_name)
        data_file = open(f'{sys.path[0]}/{file_name}', 'a')
        csv_writer = csv.writer(data_file)
6ss1mwsb

6ss1mwsb1#

您看到的: strtype hint,在Python 3.5中引入。
如果你不能升级你的项目来使用Python 3.5(这是可以理解的,这不是一个从Python 2.7.13升级的小问题),你可以删除这些提示:

def create_file_if_not_exist(file_name):
    # Type hint removed here ---------^

相关问题