首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Windows Shell启动python脚本?

问如何从Windows Shell启动python脚本?
EN

Stack Overflow用户
提问于 2018-05-04 22:08:36
回答 3查看 2.6K关注 0票数 0

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

为此,我在Python解释器中复制了该文件的路径,但得到错误SyntaxError: unexpected character after line continuation character

我是初学者,这是到目前为止我发现的最简单的启动Python脚本的方法…如果你有更简单的方法我愿意接受小费...

谢谢

下面是我的Python脚本:

代码语言:javascript
复制
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()
EN

回答 3

Stack Overflow用户

发布于 2018-05-04 22:11:50

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

代码语言:javascript
复制
$ python path/to/your/script.py

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

代码语言:javascript
复制
>>> execfile("path/to/your/script.py")
票数 2
EN

Stack Overflow用户

发布于 2018-05-04 22:16:16

你可以运行

代码语言:javascript
复制
python<version> <path>

示例:

代码语言:javascript
复制
Python3 test.py

从您的错误看,您似乎正确地运行了它,但代码包含编译错误。添加代码片段,这样我们就可以看到问题出在哪里

票数 2
EN

Stack Overflow用户

发布于 2018-05-07 17:35:28

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

因此,我在YouTube教程后面添加了变量path:https://www.youtube.com/watch?v=uXqTw5eO0Mw

然后,当我试图启动脚本时,我得到了一个错误“没有找到这样的文件或目录”。这是因为我没有在脚本"C:\ ...“的路径中放一个空格。不是"C:\“

最后,我收到一条消息"python.exe无法找到'main‘模块“。这是因为我需要再次使用".py“扩展名保存我的脚本。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50176925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档