0

Pythonファイルと「g1.txt」を同じディレクトリに置きました。SublimeREPL を使用しない場合、コードは正しく実行されます

def build_graph(file_name):
    new_file = open(file_name, 'r')
    n, m = [int(x) for x in new_file.readline().split()]

    graph = {}
    for line in new_file:
        # u, v, w is the tail, head and the weight of the a edge
        u, v, w = [int(x) for x in line.split()]
        graph[(u, v)] = w

    return n, graph

if __name__ == '__main__':
    print build_graph('g1.txt')

>>> >>> Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 18, in <module>
  File "<string>", line 6, in build_graph
IOError: [Errno 2] No such file or directory: 'g1.txt'
4

2 に答える 2

0

これを試して:

 import os
 build_graph(os.path.join(os.path.dirname(__file__),"g1.txt"))

スクリプトのディレクトリを g1.txt に追加します

于 2013-10-02T13:07:19.360 に答える