python def create_file_if_not_exist(file_name: str): SyntaxError: invalid syntax near colon
Question:
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
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)
Answers:
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 ---------^
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
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)
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 ---------^