很抱歉,如果标题有点令人困惑;我不确定如何说出来。我是一名初学者,正在学习我正在学习的语言学课程,我在将一些脚本转移到命令提示符时遇到了一些问题。我使用的是Windows 8.1和python 3.6.0,如果这有帮助的话。
我遇到的问题是我可以在python.exe程序中编写一个脚本,它将完全按照它应该如何工作。然后我将使用命令提示符运行完全相同的脚本,它返回的唯一内容是一个空行。
例如,我可以手动将其输入python控制台并获得正确的输出:>>>print("hello")
hello
但如果我将其保存在Notepad ++文件中并使用命令提示符执行相同的脚本,它将如下所示(请原谅我的屠宰路径娱乐):
C:\Users\username\Python>hello.py
C:\Users\username\Python>
" hello.py"是我试图使用我在Python控制台中手动输入的完全相同的代码运行的脚本,但是当我尝试打开文件时,它什么也没做。
但是,我尝试运行的每个脚本都不会发生这种情况,而且我无法弄清楚触发这种情况的原因。我没有收到任何错误,所以我不知道如何经历反复试验。这使我越来越难以在添加元素时检查我的工作,而且我不确定如何解决这个问题。如果有人有任何建议,我将非常感谢您的帮助。
答案 0 :(得分:0)
配置Windows文件关联指针
设置python.exe完全显式路径,如下面的#3中所示 正确的值在您的系统上
ASSOC .py=PythonScript
并按 输入 FTYPE PythonScript="C:\Program Files\Python\python.exe" "%1" %*
并按 输入 SET PATHEXT=.py;%PATHEXT%
并按 输入 如果您确实需要从命令行运行脚本而不告诉shell获取python脚本文件的完整显式路径,那么您需要将此脚本所在的路径添加到%PATH%
环境变量中。
SET PATH=%PATH%;C:\Program Files\Python
,其中C:\Program Files\Python
是系统中存在此值的值。现在,只需键入带有或不带文件扩展名的脚本名称,而无需对另一个目录执行CD
或显式指定python脚本的完整路径。
<强> FTYPE /?
强>
Displays or modifies file types used in file extension associations FTYPE [fileType[=[openCommandString]]] fileType Specifies the file type to examine or change openCommandString Specifies the open command to use when launching files of this type. Type FTYPE without parameters to display the current file types that have open command strings defined. FTYPE is invoked with just a file type, it displays the current open command string for that file type. Specify nothing for the open command string and the FTYPE command will delete the open command string for the file type. Within an open command string %0 or %1 are substituted with the file name being launched through the assocation. %* gets all the parameters and %2 gets the 1st parameter, %3 the second, etc. %~n gets all the remaining parameters starting with the nth parameter, where n may be between 2 and 9, inclusive. For example: ASSOC .pl=PerlScript FTYPE PerlScript=perl.exe %1 %* would allow you to invoke a Perl script as follows: script.pl 1 2 3 If you want to eliminate the need to type the extensions, then do the following: set PATHEXT=.pl;%PATHEXT% and the script could be invoked as follows: script 1 2 3
<强> ASSOC /?
强>
Displays or modifies file extension associations ASSOC [.ext[=[fileType]]] .ext Specifies the file extension to associate the file type with fileType Specifies the file type to associate with the file extension Type ASSOC without parameters to display the current file associations. If ASSOC is invoked with just a file extension, it displays the current file association for that file extension. Specify nothing for the file type and the command will delete the association for the file extension.