因此,当我使用命令 ./randline.py test.txt 在 python 2.7.3 中运行此代码时,此代码可以正常工作。但是,当我尝试在 python 3 中运行此代码时,我收到一条错误消息“/usr/bin/python:无法打开文件 '3':[Errno 2] 没有这样的文件或目录”
import random, sys
from optparse import OptionParser
class randline:
def __init__(self, filename):
f = open(filename, 'r')
self.lines = f.readlines()
f.close()
def chooseline(self):
return random.choice(self.lines)
def main():
version_msg = "%prog 2.0"
usage_msg = """%prog [OPTION]... FILE
Output randomly selected lines from FILE."""
parser = OptionParser(version=version_msg,
usage=usage_msg)
parser.add_option("-n", "--numlines",
action="store", dest="numlines", default=1,
help="output NUMLINES lines (default 1)")
options, args = parser.parse_args(sys.argv[1:])
try:
numlines = int(options.numlines)
except:
parser.error("invalid NUMLINES: {0}".
format(options.numlines))
if numlines < 0:
parser.error("negative count: {0}".
format(numlines))
if len(args) != 1:
parser.error("wrong number of operands")
input_file = args[0]
try:
generator = randline(input_file)
for index in range(numlines):
sys.stdout.write(generator.chooseline())
except IOError as (errno, strerror):
parser.error("I/O error({0}): {1}".
format(errno, strerror))
if __name__ == "__main__":
main()
python 3解释器的这段代码有什么问题吗?