我正在尝试用 Python 读取 YAML 文件,但由于路径中的“\”,它认为该文件是十六进制数,因此失败,这是我的代码
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))
Run Code Online (Sandbox Code Playgroud)
错误信息:
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
Run Code Online (Sandbox Code Playgroud)
我尝试用正斜杠和反斜杠给出路径。甚至尝试使用 os.path 但没有任何效果。相同的代码在 Mac 上运行良好,但在 Windows 上失败。
yaml 文件内容
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" …Run Code Online (Sandbox Code Playgroud) 我有一个带有以下行的脚本
sshpass -p 'password' ssh user@server "touch /tmp/test"
Run Code Online (Sandbox Code Playgroud)
当我运行脚本时,这是有效的,但如果我将脚本放入 cron 中,它就不起作用。有任何想法吗 ?