我在哪里保存文件,以便python在windows powershell中执行它



我开始学习python,我正在使用的电子书是"艰难地学习python"之类的东西。

我已经很好地完成了练习0-19,但我被困在练习2o,我请求您帮助解决如何将文件保存在脚本或相关文件中的问题,以便当我在windows powershell中键入时,我得到文件是什么。

这个问题对你来说可能很愚蠢,但对我很有用。请帮帮我。查看脚本中的评论。

这是python脚本:

from sys import argv
script, input_file = argv
def print_all(f):
    print f.read()
    def rewind(f):
        f.seek(0)
def print_a_line(line_count, f):
    print line_count, f.readline()
current_file = open(input_file) # Is it here where should I put the file or if yes how?
print "First let's print the whole:n"
print_all(current_file)
print "Let's rewind, kind of like a tape."
rewind(current_file)
print "Let's print three lines:"
current_line = 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)

这是我得到的错误。

PS D:\pythoprograms>python ex20.py adsense.txt追踪(最近一次通话):文件"ex20.py",第3行,位于脚本,adsense.txt=argvNameError:未定义名称"adsense"PS D:\pythoprograms>python ex20.py adsense.txt追踪(最近一次通话):文件"ex20.py",第14行,位于current_file=open(input_file,adsense.txt)#它在这里吗?我应该把文件放在哪里?如果是,怎么放?NameError:未定义名称"adsense"PS D:\pythoprograms>python ex20.py input_file追踪(最近一次通话):文件"ex20.py",第14行,位于current_file=open(input_file,"adsense.txt","r")#我应该把文件放在哪里?如果是,怎么放?TypeError:需要一个整数PS D:\ pythonprograms>

您得到的错误是什么?您需要提供文件的确切位置,或者确保您的python文件和文本文件都在同一目录中。

是的,这是文件名的正确位置,但必须将"或"与filename-'filename'一起使用。在您的代码python中,try-fiund变量的名称是adsense。

最新更新