I am trying to read a YAML file in Python, but due to '\' in the path it is considering that as a hexadecimal number and hence failing, Here is my code
import yaml
def parse_yaml(file_path):
with open(file_path) as stream:
yaml_dict = None
try:
yaml_dict = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
return yaml_dict
file_path = r"C:\\Users\\user\\PycharmProjects\\testautomation\\conf_windows\\generic_configs.yml"
print(parse_yaml(file_path))
Error message:
Error
while scanning a double-quoted scalar
in "C:\\Users\\user\\PycharmProjects\\testautomation\\conf_windows\\generic_configs.yml", line 2, column 16
expected escape sequence of 8 hexdecimal numbers, but found 's'
in "C:\\Users\\user\\PycharmProjects\\testautomation\\conf_windows\\generic_configs.yml", line 2, column 21
None
I tried giving path in forward slash and backward slash. Even tried using os.path but nothing worked. The same code is working fine on Mac but failing on Windows.
yaml file content
batchwrite:
input_file : "/Users/user/Documents/Codes/testautomation/input/batch_write_input.xlsx"
output_path : "/Users/user/Documents/Codes/testautomation/output"
dml_file : "/Users/user/Documents/Codes/testautomation/conf/info.dml"
file_type_yml : "/Users/user/Documents/Codes/testautomation/conf/fields.yml"