gpt4 book ai didi

python - 语法无效?

转载 作者:行者123 更新时间:2023-11-28 22:15:09 28 4
gpt4 key购买 nike

出于某种原因,我在 Debian 上的代码中不断收到无效语法错误。但是当我在我的 Mac 上运行它时,没有任何错误发生并且运行顺利。你们能帮帮我吗?

def read_config(cfg='~/instacron/config.txt'):
"""Read the config.

Create a config file at `cfg` with the
following information and structure:
my_user_name
my_difficult_password
"""
import os.path
_cfg = os.path.expanduser(cfg)
try:
with open(_cfg, 'r') as f:
user, pw = [s.replace('\n', '') for s in f.readlines()]
except Exception:
import getpass
print(f"\nReading config file `{cfg}` didn't work")
user = input('Enter username and hit enter\n')
pw = getpass.getpass('Enter password and hit enter\n')
save_config = input(
f"Save to config file `{cfg}` (y/N)? ").lower() == 'y'
if save_config:
os.makedirs(os.path.dirname(_cfg), exist_ok=True)
with open(_cfg, 'w') as f:
f.write(f'{user}\n{pw}')
return {'username': user, 'password': pw}`
 print(f"\nReading config file `{cfg}` didn't work")
^
SyntaxError: invalid syntax

最佳答案

f 字符串 (f"{var}") 仅在 Python 3.6 版本中添加;早期版本不接受它们。您的 Debian 和 Mac 显然运行着不同版本的 Python,只有一个至少是 3.6。

关于python - 语法无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52974768/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com