由于某种原因,我在 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}`
Run Code Online (Sandbox Code Playgroud)
print(f"\nReading config file `{cfg}` didn't work")
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
f-strings ( f"{var}"
) 仅在 Python 3.6 版本中添加;早期版本不接受它们。您的 Debian 和 Mac 显然运行着不同版本的 Python,其中只有一个版本至少为 3.6。
归档时间: |
|
查看次数: |
184 次 |
最近记录: |