IOError: [Errno 2] No such file or directory错误出现在输出文件中。

3
我正在编写一个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'将成为输出文件,我建议在需要时创建该文件以防文件不存在。在我将此代码放入类之前,它曾经起作用过...


你在哪里调用sort、reverse或shuffle?我无法重现你的错误。此外,我不明白为什么你要使用%s来写入文件,item已经是一个字符串了,而且为什么要用“wb”打开?最后,请参考这个问题以了解读取源文件的其他方法。 - Mel
我目前仍未调用sort、reverse或shuffle,只是通过创建“Xiv”类的“x”实例来调用__init__。我使用%s和“wb”,因为我从某个我找到的示例中获取并且之前它可以工作;) 无论如何,我的问题是为什么输出文件出现错误?它不是要从磁盘读取的源文件... - Prophet
实际上,为了让它工作,我必须删除 type=file。它给了我一个 NameError: name 'file' is not defined 的错误。 - Mel
请展示完整的回溯信息。这包括有价值的信息,例如错误发生的行。此外,请注意您的sort()方法将引发NameError异常,因为source变量未定义。 - holdenweb
这个问题 如何使用argparse打开文件? 表明type=file并不是你该做的事情。 - Mel
回溯信息在C:\Python27\lib\argparse.py文件中。这些信息对您有帮助吗?我的代码唯一的参考是以下代码行:第45行的x = Xiv()`。我看到了您提到的那个问题,但没有找到解决我的问题的答案;( - Prophet
3个回答

1

我遇到了这个错误:

jemdoc index Traceback (most recent call last): File "/usr/bin/jemdoc", line 1563, in main() File "/usr/bin/jemdoc", line 1555, in main infile = open(inname, 'rUb') IOError: [Errno 2] No such file or directory: 'index.jemdoc'

然后我进行了更改。

infile = open(inname, 'rUb') 

to

infile = open(inname, 'w+')

然后错误问题得到了解决。

1
嗨,这似乎是针对jemdoc特定问题的不同答案。 - Luna
1
看起来你已经回答了我的问题。不过,我有一个建议:在回答时尽量更具体一些。并且使用不同的格式,例如突出代码。我为您做了一些修改 :) - SamWhan

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

1
< p > open 函数默认以读取模式打开文件。您需要的是以写/创建模式打开它,这将在文件不存在时创建它并允许您写入它:

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

第二个参数指定打开模式,默认为'r',表示只读模式。
我不确定为什么在将其移入类之前它可以工作 - 按照所有标准,这应该没有任何区别。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接