【问题标题】:IOError: [Errno 2] No such file or directory error is presented for the ouptup fileIOError: [Errno 2] No such file or directory error is presented for the ouptup file
【发布时间】:2016-05-27 23:14:39
【问题描述】:

我正在编写一个 Python 代码来操作文本文件。代码将从命令行接收输入文件和输出文件名和标志 -sort、-reverse 等,根据操作应用于输入文件,最后将数据写入输出文件。我需要在一个类中完成所有这些工作,以便代码可以继承。到目前为止,我有这样的代码:

import argparse  
import random  

class Xiv(object):  

    def __init__(self):  
        parser = argparse.ArgumentParser()  
        group = parser.add_mutually_exclusive_group()  
        group.add_argument("-s", "-sort", action="store_true")  
        group.add_argument("-r", "-reverse", action="store_true")  
        group.add_argument("-sh", "-shuffle", action="store_true")  
        parser.add_argument("inputfile", type = file, help="Input file name")  
        parser.add_argument("outputfile", type = file, help="Output file name")  
        args = parser.parse_args()  
        source =args.inputfile  
        dist = args.outputfile  

    def sort(self):  
     f = open(source, "r")  
     list1 = [line for line in f if line.strip()]  
     f.close()  
     list.sort()  
     with open(dist, 'wb') as fl:  
       for item in list:  
        fl.write("%s" % item)  

    def reverse(self, source, dist):  
     f = open(source, "r")  
     list2 = [line for line in f if line.strip()]  
     f.close()  
     list2.reverse()  
     with open(dist, 'wb') as f2:  
       for item in list2:  
         f2.write("%s" % item)      

def shuffle(self, source, dist):  
    f = open(source, "r")  
    list3 = [line for line in f if line.strip()]  
    f.close()  
    random.shuffle(list3)  
    with open(dist, 'wb') as f3:  
     for item in list3:  
      f3.write("%s" % item)  

x = Xiv();  

现在当我运行它时

python xiv.py -s text.txt out.txt 

出现以下错误

IOError: [Errno 2] No such file or directory 'out.txt'  

但是'out.txt' 将是输出文件,我建议创建它的代码以防文件不存在。在我把这段代码放到类中之前它就起作用了......

【问题讨论】:

  • 你在哪里调用排序、反向或随机播放?我无法重现您的错误。另外,我不明白你为什么使用 %s 写入文件,item 已经是一个字符串,为什么你用 "wb" 打开?最后查看this question了解其他读取源文件的方式。
  • 我目前还没有调用 sort、reverse 或 shuffle,只是通过创建 'Xiv' 类的 'x' 实例来调用 init。我正在使用 %s 和“wb”,因为我从我找到的一些示例中获取它并且它之前工作过;)无论如何,我的问题是为什么输出文件会出现错误?它不是要从磁盘读取的源文件..
  • 实际上要让它工作,我必须删除type=file。它给了我NameError: name 'file' is not defined
  • 请显示整个回溯。这包括有价值的信息,例如发生错误的行。另外,请注意您的sort() 方法将引发NameError 异常,因为source 变量将未定义
  • 这个问题How to open file using argparse?暗示type=file不是你应该做的。

标签: python argparse


【解决方案1】:

在 Python 2 中,当我在一个不存在的文件上调用 file 时,我收到此错误:

In [13]: file('out.txt')
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-13-d0d554b7d5b3> in <module>()
----> 1 file('out.txt')

IOError: [Errno 2] No such file or directory: 'out.txt'

在 Py2 中,file 等价于 open。它会打开一个文件,默认为r 模式,因此如果文件不存在就会报错。

argparse 中,type=foo 表示使用输入字符串运行函数foo。这并不意味着“将字符串解释为这种类型的对象”。

在您的代码中:

with open(dist, 'wb') as f2:  
   for item in list2:  
       f2.write("%s" % item)

这意味着您希望dist 是一个文件名,一个字符串。您不希望解析器首先打开文件。你自己正在这样做。所以不要指定type 参数。

@tmoreau - Python3 删除了 file 函数,只留下了 open

【讨论】:

    【解决方案2】:

    open 函数默认打开一个文件进行读取。您想要的是在写入/创建模式下打开它,如果它不存在,它将创建它并允许您对其进行写入:

    with open(dist, 'w+') as f3:
    

    第二个参数指定打开模式,默认为'r',表示只读。

    我不确定为什么在您将它移入课堂之前它会起作用 - 从各方面来说,它应该没有任何区别。

    【讨论】:

      【解决方案3】:

      我遇到了这个错误..

      jemdoc 索引 回溯(最近一次通话最后): 文件“/usr/bin/jemdoc”,第 1563 行,在 主要的() 文件“/usr/bin/jemdoc”,第 1555 行,在 main infile = open(inname, 'rUb') IOError:[Errno 2] 没有这样的文件或目录:'index.jemdoc'

      那我变了

      infile = open(inname, 'rUb') 
      

      infile = open(inname, 'w+')
      

      然后错误就解决了。

      【讨论】:

      • 嗨,这似乎是对 jemdoc 特有的不同问题的回答。
      • 您似乎已经回答了我这个问题。但是,一个建议:尽管在回答时尝试更具体。并使用不同的格式,例如让代码脱颖而出。我冒昧地为你做了一些:)
      猜你喜欢
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      相关资源
      最近更新 更多