如何使用Python解释器启动python脚本?

时间:2018-05-04 14:08:36

标签: python launch

我想启动一个python脚本(file.py)

为了做到这一点,我在Python解释器中复制了这个文件的路径,但是我得到了错误SyntaxError: unexpected character after line continuation character

我是初学者,这是我发现到目前为止发现的Python脚本最简单的方法...如果你有一个更简单的方法我会接受提示......

谢谢

这是我的Python脚本:

import os
import csv

ACCEPTED_MSG="""
Hi {},

We are thrilled to let you know that you are accepted to our 
programming workshop.

Your coach is {}.

Can't wait to see you there ! 

Thank you,

Workshop Organizers
"""

REJECTED_MSG="""
Hi {},

We are verry sorry to let you know that due to a big number
of applications we couldn't fit you at the workshop this time...

We hope to see you next time.

Thank you, 

Workshop Organizers
"""

path_to_file = "C:/Users/Julien/Downloads/data.csv"

file_exists = os.path.exists(path_to_file)

if file_exists:

    csv_file = open(path_to_file)
    csv_reader = csv.reader(csv_file, delimiter=',')
    next(csv_reader)

    for row in csv_reader:
        name, email, accepted, coach, language = row
        print (name, email, accepted, coach, language)

        if accepted =='Yes':
            msg = ACCEPTED_MSG.format(name, coach)
        else:
            msg = REJECTED_MSG.format(name)

            print ("Send e-mail to: {}".format(email))
            print("E-mail content:")
            print (msg)
csv_file.close()

3 个答案:

答案 0 :(得分:2)

如果你只想运行一个脚本,那么语法是(来自你的 system__shell,__ not python shell):

$ python path/to/your/script.py

如果你想在现有的python shell中执行,你可以运行(在python shell中):

>>> execfile("path/to/your/script.py")

答案 1 :(得分:2)

你可以运行

python<version> <path>

示例:

Python3 test.py
从你的错误

看起来你正确运行它但代码包含编译错误。添加代码段,以便我们可以看到问题在哪里

答案 2 :(得分:0)

我无法从Windows控制台启动Python脚本,因为我还没有添加变量PATH。

所以我在YouTube教程之后添加了变量路径: https://www.youtube.com/watch?v=uXqTw5eO0Mw

然后,我收到错误&#34;没有找到这样的文件或目录&#34;当我试图启动脚本时。那是因为我没有在脚本的路径中放置一个空格&#34; C:\ ...&#34;而不是&#34; C:\&#34;

最后,我收到了一条消息&#34; python.exe无法找到&#39; main &#39;模块&#34 ;.这是因为我需要使用&#34; .py&#34;来保存我的脚本。再次延长。