擅长:python、mysql、java
<p>文件必须位于运行脚本的同一文件夹中</p>
<p>如果您在<code>C:/myscript.py</code>运行,那么您的文件也需要在<code>C:/</code>运行。你知道吗</p>
<p>例如:</p>
<pre><code>>> cd C:
>> dir
ex15.txt myscript.py
>> python myscript.py ex15.txt
Here's your file: 'ex15.txt'
Something that is in your text file
type the filename again:
> ex15.txt
Something that is in your text file
</code></pre>
<p>而且,您的代码似乎是错误的。您需要使用“ex15.txt”,而不是没有引号的ex15.txt。否则它将被解释为变量,而不是字符串。你知道吗</p>
<p>请参见下面的代码:</p>
<pre><code>from sys import argv
script, filename = argv
txt = open("ex15.txt")
print "Here's your file: %r" % "ex15.txt"
print txt.read()
print "type the filename again: "
again = raw_input("> ")
again2 = open(again)
print again2.read()
</code></pre>