Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python2.7.3中工作正常,但在Python3中出现错误_Python_Python 3.x - Fatal编程技术网

在Python2.7.3中工作正常,但在Python3中出现错误

在Python2.7.3中工作正常,但在Python3中出现错误,python,python-3.x,Python,Python 3.x,因此,当我在Python2.7.3中使用命令./randline.py test.txt运行此代码时,此代码运行良好。但是,当我尝试在python 3中运行此代码时,我收到一条错误消息/usr/bin/python:无法打开文件“3”:[Errno 2]没有这样的文件或目录 import random, sys from optparse import OptionParser class randline: def __init__(self, filename): f

因此,当我在Python2.7.3中使用命令./randline.py test.txt运行此代码时,此代码运行良好。但是,当我尝试在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解释器的这段代码有什么问题吗?

看起来您在运行python 3时使用了空格。您应该改用python3。

由于python2.7.3和python3.*具有不同的语法,代码使用python2.7.3工作,然后可能无法使用python3.*。比如说

打印“hello world” 使用python2.7.3工作,但使用python3.*接收错误。要运行它,我们必须使用打印“Hello world”


所以只需将语法更改为python3.*格式。但是我的建议是使用python2.7.3来运行代码

你能修正你的缩进吗?看看你说的是/usr/bin/python3而不是/usr/bin/python3,如果我删除了我收到的消息-bash:./randline.py:/usr/bin/python3:bad解释器:没有这样的文件或目录你安装了python3吗?我如何检查是否安装了它?我在学校里使用电脑。我认为默认值是2.7.3,但不确定3。然后,根据$PATH中是否有/usr/local/cs/bin,您可以键入python3.3./randline.py test.txt或/usr/local/cs/bin/python3.3./randline.py test.txt。要查看$PATH,可以执行echo$PATH。好的。。。这很奇怪。当我直接在终端中输入命令python3时,它只需查找即可工作。但是,当我在代码中更改路径时,它根本不起作用。我会联系服务台的。thx人。顺便问一下,我的代码有什么问题吗?