【问题标题】:Python No such file or directory: 'file_name.txt\r"Python 没有这样的文件或目录:'file_name.txt\r"
【发布时间】:2014-05-11 23:53:19
【问题描述】:

我正在尝试使用三个脚本在远程 SGE 前端服务器上以“批处理”模式运行单个例程(下面代码中的例程)。但是,出于某种原因,服务器给了我一个回溯,说我试图读入的文件不存在:

Traceback (most recent call last):
  File "test.py", line 102, in <module>
    __main__()
  File "test.py", line 77, in __main__
    with open( str(file_two) ) as open_two:
IOError: [Errno 2] No such file or directory: 'A00005.txt\r'

文件名确实是“A00005.txt”,没有末尾的 \r 位,并且与下面的脚本位于同一目录中 - 有谁知道为什么将“\r”添加到文件中小路?我将在下面发布整个代码:

import sys, re

#
# Program mainline.
#
def __main__():
    num_runs = 0

    # Parse command line arguments.
    i = 1
    while i < len(sys.argv):
        arg = sys.argv[i]
        if i == 1:
            num_runs = int(arg)
        i += 1

    # In our first execution of the process outlined below, the iteration_number = 0; in the second pass, iteration_number = 1, and so on
    iteration_number = ""
    iteration_number += str(num_runs)

    ##########################################################################################################################################################
    # Using that iteration number, we can divide our routine into all of its constitive components, each of which can be run independently and in parallel
    # Our task, from this point forward, is roughly: read in the "parallel_processing_iteration_schedule" file to determine which two files are to be compared in this iteration
    # Read in those two files, run our matching algorithm, and write out the matching results.
    ##########################################################################################################################################################

    #1) define our parsing functions
    def to_words(text):
        #Break text into a list of words without punctuation
        return re.findall(r"[a-zA-Z']+", text)

    def match(a, b):
        # Make b the longer list.
        if len(a) > len(b):
            a, b = b, a
        # Map each word of b to a list of indices it occupies.
        b2j = {}
        for j, word in enumerate(b):
            b2j.setdefault(word, []).append(j)
        j2len = {}
        nothing = []
        unique = set() # set of all results
        def local_max_at_j(j):
            # maximum match ends with b[j], with length j2len[j]
            length = j2len[j]
            unique.add(" ".join(b[j-length+1: j+1]))
        # during an iteration of the loop, j2len[j] = length of longest
        # match ending with b[j] and the previous word in a
        for word in a:
            # look at all instances of word in b
            j2lenget = j2len.get
            newj2len = {}
            for j in b2j.get(word, nothing):
                newj2len[j] = j2lenget(j-1, 0) + 1
            # which indices have not been extended?  those are
            # (local) maximums
            for j in j2len:
                if j+1 not in newj2len:
                    local_max_at_j(j)
            j2len = newj2len
        # and we may also have local maximums ending at the last word
        for j in j2len:
            local_max_at_j(j)
        return unique

    # 2) Read in metadata file. Because this file is huge (1.4 billion lines long), we'll only read the row in which we're currently interested
    with open("reduced_iteration_schedule.txt") as lookup_table:
        for i, line in enumerate(lookup_table):
            if i == int(iteration_number):

                #the rows in the iteration schedule look like this: iteration_number    text_one    text_two, so let's grab values and delete trailing newline character
                split_line = line.split("\t")
                file_one = split_line[1]
                file_two = split_line[2].replace("\n","")

                with open( str(file_one) ) as open_one:
                    with open( str(file_two) ) as open_two:

                        read_one = open_one.read()
                        read_two = open_two.read()

                        matches = match( to_words(read_one), to_words(read_two) )

            #if we made it to the line beyond our current iteration number, we can stop reading that line, as we've found what we're looking for
            elif i > int(iteration_number):
                break

    # Now we have a list object named "matches" that contains all identified matches between the two texts compared in this iteration.
    # Let's reduce this list so that it contains only those matches that are three words or longer
    reduced_matches = []
    for i in matches:
        if len(i.split()) > 2:
            reduced_matches.append(i)

    # We want to write that to disk, but we can't simply write all matches for all 1.5 billion lookups to the same file, or we'll never be able to open the thing.
    # Let's write the first n iterations to one file (using "append" rather than "write"), then write the next n iterations to a different file

    if int(iteration_number) < 100:
        with open("eebo_string_comparison_1.txt","a") as out:
            out.write( str(file_one) + "\t" + str(file_two) + "\t" + str(reduced_matches) + "\n" )

__main__()

【问题讨论】:

    标签: python io path sungridengine


    【解决方案1】:

    看起来像carriage return 留在那儿。一些平台使用CRLFCRLF。看起来它可能会剥离一个而不是另一个。

    如果该字符存在,您的to_words 函数可能会将其留在那里。 \r 是否一直存在?还是只针对那个文件?

    作为一种选择,您可以添加代码以从文件名标记中去除所有尾随 CR 和 LF 字符。 (你不想去掉所有的空格,因为在某些平台上允许以空格结尾的文件。)

    【讨论】:

    • 谢谢@Ken;我相信 \r 一直存在,但我正在等待风暴过去,然后再打开带有详细信息的笔记本电脑。 (我是从 2002 年的一个简单的 XP 盒子开始写的!)
    【解决方案2】:

    替换:

    split_line[2].replace("\n","")
    

    与:

    split_line[2].rstrip("\n\r")
    

    回车,\r,是 dos/windows 系统上使用的行尾的一部分,经常在不需要的地方弹出。

    【讨论】:

    • 我认为你们是男的!谢谢你们的回答。在我点击我的 SO 收件箱的那一刻,我听到了雷声,所以我要等到海岸干净后再运行带有更改的脚本...
    • 哇哦!我们有钱了!万岁!
    猜你喜欢
    • 2013-10-25
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多