IOError: [Errno 2] No such file or directory error is presented for the ouptup file

IOError: [Errno 2] No such file or directory error is presented for the ouptup file

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

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' 将成为输出文件,我建议使用代码创建它以防文件不存在。在我将此代码放入 class.....

之前它起作用了

在 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使用输入字符串。这并不意味着 'interpret the string as an object of this type'.

在您的代码中:

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

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

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

open函数默认打开一个文件进行读取。你想要的是以 write/create 模式打开它,如果它不存在就会创建它并允许你写入它:

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

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

我不确定为什么在您将其移至 class 之前它会起作用 - 从各方面来看,它应该没有什么区别。

我收到这个错误..

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') 

infile = open(inname, 'w+')

然后错误解决了。