user2437501
user2437501

Reputation: 21

Syntax invalid?

For some reason I keep getting an invalid syntax error of my code on Debian. But when I run it on my mac nothing wrong happens and it runs smooth. Could you guys help me?

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

Upvotes: 2

Views: 214

Answers (1)

jwodder
jwodder

Reputation: 57650

f-strings (f"{var}") were only added to Python in version 3.6; earlier versions do not accept them. Your Debian and Mac are clearly running different versions of Python, only one of which is at least 3.6.

Upvotes: 5

Related Questions