Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python def create_file_if_not_exist(file_name: str): SyntaxError: invalid syntax near colon

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

I’m getting this error when I run python report.py

Can someone help me find out what m i doing wrong

I tried removing : str from "def create_file_if_not_exist(file_name: str)"but the error passes to next line

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

The : str you’re seeing there is a type hint, introduced in Python 3.5.

If you cannot upgrade your project to use (at least) Python 3.5 (which is understandable, it’s not a trivial upgrade from 2.7.13), you could just remove these hints:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading