我遇到了一个问题,即python没有为代码中的输入调用输入输入。它只是跳过获取输入并将一个空字符串放入变量源,这会导致程序在尝试打开名为source(空字符串)的文件时失败。
可能导致它跳过一行输入的原因是什么?
我有这段代码我写的只是几行要求用户输入。
def main():
source = input('please enter source file name: ')
file_name = input('please enter file name you want script to be saved as: ')
language = input('please enter language to generate script in: ')
args = input('please enter comma delimited list of attributes from source file (if any): ')
print('\nGenerating script...\n')
generate_script(source,file_name,args,language)
return
当我运行程序时,它正常工作并输出
please enter source file name: Tree.txt
please enter file name you want script to be saved as: t
please enter language to generate script in: matlab
please enter comma delimited list of attributes from source file (if any):
Generating script...
Generation successful
当我将此代码交给其他人并且他们在计算机上运行时发生此错误
please enter source file name: /* SHOULD TAKE USER INPUT HERE BUT IMMEDIATELY PRINTS NEXT INPUT LINE INSTEAD */ please enter file name you want script to be saved as: 'C:\Users\pmade\Desktop\DecisionTreeGenerator-master\DecisionTreeGenerator-master\T.txt'
please enter language to generate script in: 'matlab'
please enter comma delimited list of attributes from source file (if any): 'PCI0, FREEZE_THAW_YR'
Generating script...
File "<stdin>", line 1, in <module>
File "C:\Program Files\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Program Files\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/pmade/Desktop/DecisionTreeGenerator-master/DecisionTreeGenerator-master/Text2Code.py", line 162, in <module>
main()
File "C:/Users/pmade/Desktop/DecisionTreeGenerator-master/DecisionTreeGenerator-master/Text2Code.py", line 9, in main
generate_script(source,file_name,args,language)
File "C:/Users/pmade/Desktop/DecisionTreeGenerator-master/DecisionTreeGenerator-master/Text2Code.py", line 32, in generate_script
with io.open(source,'r') as f_r:
FileNotFoundError: [Errno 2] No such file or directory: "runfile('C:/Users/pmade/Desktop/DecisionTreeGenerator-master/DecisionTreeGenerator-master/Text2Code.py', wdir='C:/Users/pmade/Desktop/DecisionTreeGenerator-master/DecisionTreeGenerator-master')"
>>> Traceback (most recent call last):
似乎解释器在main中打印出第一个输入调用的消息,然后在等待输入之前直接进入第二个输入调用,导致它抱怨该文件名为&#34;&#34;在当前目录中不存在。任何人都知道发生了什么?
编辑:抱歉忘记解释正确,函数generate_script使用从跳过的输入中取出的字符串来打开要读取的源文件。 这是代码:def generate_script(source,file_name,args='',language='python'):
with io.open(source,'r') as f_r:
do work with file f_r....
由于错误,解释器会跳过通常放入源变量的输入,导致io尝试打开名为&#34;&#34;的文件。由于来源为空白,导致上述错误。
答案 0 :(得分:0)
错误消息的最后一行几乎说明了一切:
FileNotFoundError:[Errno 2]没有这样的文件或目录:&#34; runfile(&#39; C:/Users/pmade/Desktop/DecisionTreeGenerator-master/DecisionTreeGenerator-master/Text2Code.py' ;, wdir =&#39; C:/用户/ pmade /桌面/ DecisionTreeGenerator主/ DecisionTreeGenerator主&#39;)&#34;
错误发生在函数generate_script
中,该函数不是您发布的代码的一部分。
答案 1 :(得分:0)
看起来错误发生在第7行,您调用&#34; generate_script&#34;。根据具体情况以及您可能调用的模块,其他人可能没有安装这些模块,或者没有正确调用它们。