[Python] Getting a Python Invalid Syntax error when I run a short python script

Discussão em 'Python' iniciado por Stack, Janeiro 29, 2025.

  1. Stack

    Stack Membro Participativo

    I am writing a Python script that will allow me to access and analyze my Strava data which is stored in a activites.json file in the same directory as my script. (I haven't coded the "analyze" part yet.) I am using VS Code.

    Here's the code:

    import json
    import os

    for file in os.chdir('data_project'):
    with open(f'activities.json{file}', 'r') as f:
    data = json.load(f)
    print(data)


    This is the error I receive:

    >>> /Users/nicholas/Python_Projects/.venv/bin/python /Users/nicholas/Python_Projects/data_project/strava_analysis.py
    File "<stdin>", line 1
    /Users/nicholas/Python_Projects/.venv/bin/python /Users/nicholas/Python_Projects/data_project/strava_analysis.py
    ^
    SyntaxError: invalid syntax


    And here is a screenshot of my working directory in VS Code: (https://i.sstatic.net/V0sTrxht.png)

    I've been having real trouble in general getting my .py scripts to access the correct directory that my .json and .txt files are stored in, even though they are always stored in the same directory as my .py scripts.

    I tried changing the path to the file's directory with the: "for file in os.chdir('data_project')" command but that led do the current error I'm receiving.

    Ideas (and suggestions for where to learn more about Python directory behavior) welcome.

    Continue reading...

Compartilhe esta Página