语法无效?

时间:2018-10-24 17:23:13

标签: python syntax

由于某种原因,我在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

1 个答案:

答案 0 :(得分:4)

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