在哪里保存文件,以便python在windows power shell中执行

在哪里保存文件,以便python在windows power shell中执行,python,python-2.7,Python,Python 2.7,我开始学习python,我正在使用的电子书是“艰苦学习python”之类的东西 我已经很好地完成了练习0-19,但是我被困在练习2o中,我请求您帮助我解决如何将文件保存在脚本或相关文件中的问题,以便我在windows power shell中键入时获得文件内容 这对你们来说可能是个愚蠢的问题,但对我很有用,请帮帮我。检查脚本中的注释 以下是python脚本: from sys import argv script, input_file = argv def print_all(f):

我开始学习python,我正在使用的电子书是“艰苦学习python”之类的东西

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

这对你们来说可能是个愚蠢的问题,但对我很有用,请帮帮我。检查脚本中的注释

以下是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:\pythonprograms>python ex20.py adsense.txt 回溯(最近一次呼叫最后一次): 文件“ex20.py”,第3行,在 脚本,adsense.txt=argv NameError:未定义名称“adsense” PS D:\pythonprograms>python ex20.py adsense.txt 回溯(最近一次呼叫最后一次): 文件“ex20.py”,第14行,在 当前_file=open(input_file,adsense.txt)#在这里我应该把文件放在哪里?如果是,怎么放? NameError:未定义名称“adsense” PS D:\pythonprograms>python ex20.py输入文件 回溯(最近一次呼叫最后一次): 文件“ex20.py”,第14行,在 当前_文件=打开(输入_文件,“adsense.txt”,“r”)#在这里我应该把文件放在哪里?如果是,怎么放? TypeError:需要一个整数
PS D:\pythonprograms>

您遇到的错误是什么?您需要给出文件的确切位置,或确保python文件和文本文件位于同一目录中。

是的,这是文件名的正确位置,但必须使用“”或“”作为文件名-“文件名”。
在您的代码中,python try find variable,它的名称是adsense。

我已经尝试运行了好几次,但都没有效果,下面是错误。PS D:\pythonprograms>python ex20.py adsense.txt回溯(最后一次调用):文件“ex20.py”,第3行,在脚本中,adsense.txt=argv name错误:名称“adsense”未定义PS D:\pythonprograms>python ex20.py adsense.txt回溯(最后一次调用):文件“ex20.py”,第14行,当前_文件=打开(输入_文件,adsense.txt)#在这里我应该把文件放在哪里?如果是,怎么放?NameError:未定义名称“adsense”